diff --git a/nand-loader/COPYING.tinyload b/nand-loader/COPYING.tinyload deleted file mode 100644 index 08ddefd..0000000 --- a/nand-loader/COPYING.tinyload +++ /dev/null @@ -1,340 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. - diff --git a/nand-loader/Makefile b/nand-loader/Makefile index 859cd8d..652aaa6 100644 --- a/nand-loader/Makefile +++ b/nand-loader/Makefile @@ -1,28 +1,134 @@ -ifeq ($(strip $(DEVKITPPC)),) -$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") +#--------------------------------------------------------------------------------- +# Clear the implicit built in rules +#--------------------------------------------------------------------------------- +.SUFFIXES: + +#--------------------------------------------------------------------------------- +ifeq ($(strip $(DEVKITPPC)),) +$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC)") endif include $(DEVKITPPC)/wii_rules -.PHONY = all clean -CURDIR_TMP := $(CURDIR) +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# INCLUDES is a list of directories containing extra header files +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := -all: - @echo Make Stub - @$(MAKE) --no-print-directory -C $(CURDIR_TMP)/stub \ - -f $(CURDIR_TMP)/stub/Makefile - @mv -u $(CURDIR_TMP)/stub/stub.bin \ - $(CURDIR_TMP)/make_app/stub.bin - @echo Make NAND Loader - @$(MAKE) --no-print-directory -C $(CURDIR_TMP) \ - -f $(CURDIR_TMP)/Makefile.loader - @mv -u $(CURDIR_TMP)/loader.bin \ - $(CURDIR_TMP)/make_app/loader.bin +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- + +CFLAGS = -O2 -mrvl -Wall $(MACHDEP) $(INCLUDE) +CXXFLAGS = $(CFLAGS) + +LDFLAGS = $(MACHDEP) -Wl,-Map,$(notdir $@).map + +#--------------------------------------------------------------------------------- +# any extra libraries we wish to link with the project +#--------------------------------------------------------------------------------- +LIBS := -lpng -lmodplay -lasnd -lwiiuse -lbte -logc -lm -lz + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := + + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export TOPDIR := $(CURDIR) + +export OUTPUT := $(CURDIR)/$(TARGET) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +export DEPSDIR := $(CURDIR)/$(BUILD) + +#--------------------------------------------------------------------------------- +# automatically build a list of object files for our project +#--------------------------------------------------------------------------------- +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) + export LD := $(CC) +else + export LD := $(CXX) +endif + +export OFILES := $(addsuffix .o,$(BINFILES)) \ + $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \ + $(sFILES:.s=.o) $(SFILES:.S=.o) + +#--------------------------------------------------------------------------------- +# build a list of include paths +#--------------------------------------------------------------------------------- +export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) \ + -I$(LIBOGC_INC) + +#--------------------------------------------------------------------------------- +# build a list of library paths +#--------------------------------------------------------------------------------- +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ + -L$(LIBOGC_LIB) + +export OUTPUT := $(CURDIR)/$(TARGET) +.PHONY: $(BUILD) clean + +#--------------------------------------------------------------------------------- +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- clean: - @$(MAKE) --no-print-directory -C $(CURDIR_TMP)/stub \ - -f $(CURDIR_TMP)/stub/Makefile clean - @$(MAKE) --no-print-directory -C $(CURDIR_TMP) \ - -f $(CURDIR_TMP)/Makefile.loader clean - rm -rf $(CURDIR_TMP)/make_app/stub.bin - rm -rf $(CURDIR_TMP)/make_app/loader.bin - rm -rf $(CURDIR_TMP)/make_app/00000001.app + @echo clean ... + @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).bin + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT).bin: $(OUTPUT).elf +$(OUTPUT).elf: $(OFILES) + +#--------------------------------------------------------------------------------- +%.bin: + $(OBJCOPY) -O binary $^ $@ + +%.elf: + @echo linking ... $(notdir $@) + $(LD) -T ../rvl.ld $^ $(LDFLAGS) $(LIBPATHS) $(LIBS) -o $@ + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- diff --git a/nand-loader/Makefile.loader b/nand-loader/Makefile.loader deleted file mode 100644 index a29008f..0000000 --- a/nand-loader/Makefile.loader +++ /dev/null @@ -1,54 +0,0 @@ -ifeq ($(strip $(DEVKITPPC)),) -$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") -endif - -include $(DEVKITPPC)/wii_rules - -PREFIX = powerpc-eabi- - -AR = $(PREFIX)ar -AS = $(PREFIX)as -CC = $(PREFIX)gcc -CXX = $(PREFIX)g++ -LD = $(PREFIX)ld -OBJCOPY = $(PREFIX)objcopy -RANLIB = $(PREFIX)ranlib -STRIP = $(PREFIX)strip - -MACHDEP = -mcpu=750 -mno-eabi -mhard-float -CFLAGS = $(MACHDEP) -O1 -Werror -Wall -pipe -ffunction-sections -finline-functions-called-once -mno-sdata -LDFLAGS = $(MACHDEP) -n -nostartfiles -nostdlib -Wl,-T,openstub.ld -L. -ASFLAGS = -D_LANGUAGE_ASSEMBLY -DHW_RVL - -TARGET_LINKED = boot.elf -TARGET = loader.bin - -CFILES = ios.c utils.c cache.c main.c -OBJS = app_booter.o crt0.o memory.o ios.o utils.o cache.o main.o - -DEPDIR = .deps - -all: $(TARGET) - -%.o: %.s - @echo " ASSEMBLE $<" - @$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@ - -%.o: %.S - @echo " ASSEMBLE $<" - @$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@ - -%.o: %.c - @echo " COMPILE $<" - @$(CC) $(CFLAGS) $(DEFINES) -c $< -o $@ - -$(TARGET_LINKED): $(OBJS) - @echo " LINK $@" - @$(CC) -g -o $@ $(LDFLAGS) $(OBJS) $(LIBS) - -$(TARGET): $(TARGET_LINKED) - @echo " OBJCOPY $@" - @$(OBJCOPY) -O binary $< $@ - -clean: - rm -rf $(TARGET_LINKED) $(TARGET) $(OBJS) $(DEPDIR) diff --git a/nand-loader/README b/nand-loader/README index 49d023f..9dbb6dc 100644 --- a/nand-loader/README +++ b/nand-loader/README @@ -1,2 +1,29 @@ -This Project is based on megazigs stub, tinyload and dimoks app loader. -With this NAND Loader you can update existing forwarders to work in WiiU vWii Mode, just unpack your wad file, replace the app file with the one in the binary folder, repack the wad and its ready to use. \ No newline at end of file ++---------------------------------+ +| [+] Custom NAND Loader v1.0 | +| developed by Waninkoko | ++---------------------------------+ +| wwww.teknoconsolas.es | ++---------------------------------+ + + +[ DESCRIPTION ]: + +- This is a custom NAND Loader for Wii channels. It is 100% legal and works + with homebrew and commercial channels. + + Also, it allows to configure the loading options for each channel. + + +[ NOTES ]; + +- If RESET button is held while booting the channel it will access + the configuration menu. + + +[ KUDOS ]: + +- Team Twiizers and devkitPRO devs. +- crediar. +- WiiGator. +- kwiirk. +- All the betatesters. diff --git a/nand-loader/README.libogc b/nand-loader/README.libogc new file mode 100644 index 0000000..a2dfc2d --- /dev/null +++ b/nand-loader/README.libogc @@ -0,0 +1 @@ +properly building this NAND-Loader does (still) require libogc from 2009 diff --git a/nand-loader/README.tinyload b/nand-loader/README.tinyload deleted file mode 100644 index 734df4b..0000000 --- a/nand-loader/README.tinyload +++ /dev/null @@ -1,131 +0,0 @@ -TinyLoad v0.2 -A Wii disc game loader in 4096 bytes -==================================== - -What? -===== - -TinyLoad is a simple original Wii game launcher. You run it, it launches -whatever's inserted into the drive. Simple. It ignores game regions, so it's -region-free. It won't install updates. It won't load burned copies. There are no -controls or settings. - -The user interface shows two things: progress, and error status. A white bar -across the bottom of the screen shows the current (rough) progress. If an error -is detected, a portion of the top of the screen will turn red. It will then -attempt to launch The Homebrew Channel (only applies to recent versions with the -JODI Title ID). If this fails then it will simply hang. - -If the launcher freezes with the progress bar visible and no red error box, then -you've probably hit a bug. Try the debug version if you have a USB Gecko. - -If it freezes with a black screen after the progress bar has reached its -endpoint and disappeared, then the game itself is freezing. - -I obviously have only tested this with a few games. Chances are it doesn't work -with every Wii game out there. - -The debug version requires a USB Gecko and shows debug messages over it. - -Notes: -- TinyLoad sets the PPC timebase correctly according to your Wii's RTC. This - fixes date/time issues in games. -- The video code makes lots of assumptions. It will only work if VI was left - configured in a "standard" mode, with a 640x480-640x574 framebuffer. VI should - be blanked; if it isn't, then TinyLoad will not blank it before launching - the game so your screen will blink green for a split second as the game - initializes VI. It has been tested to work correctly when launched by the - Homebrew Channel in at least NTSC 480p and PAL interlaced modes. If these - assumptions don't hold then the progress bar display will not work properly, - but the rest of the loader should work fine. -- TinyLoad does not perform _any_ patching of games. The lowmem video mode - setting follows whatever video mode was left set by the application used to - launch TinyLoad, except that PAL games are forced to PAL if NTSC is detected. - This does not patch the game, it's merely the informative value in low memory; - games are free to read SYSCONF/setting.txt and ignore it. I don't really care - because I use 480p mode anyway. If you need more advanced options, just use - Gecko OS. -- Normally, game audio will not work correctly if launched via a loader that - was initially launched via BootMii-boot2. This is a bug in libogc (it doesn't - know how to initialize the DSP for the first time and leaves it in a broken - state), and it affects anything running after the Wii was booted first into - a libogc application, including the System Menu's loader. In other words, - BootMii-boot2 -> HBC -> System Menu (or Gecko OS) -> Game may cause distorted - audio. TinyLoad _does_ work, by resetting the audio hardware to let the game - reinitialize it properly. So, BootMii-boot2 -> HBC -> TinyLoad -> Game will - work fine. This ought to be worked around in a future release of HBC, at - least. I'd suggest fixing libogc, but I know shagkur is just going to rip the - proper code from the SDK again. Anyway, launching using TinyLoad will work - fine as it contains the workaround. -- TinyLoad will load the correct IOS as specified in the partition TMD. It does - not support loading any other IOS. -- TinyLoad will not install updates. Not having the right IOS for the game will - probably result in a red error and reset about halfway through. - -Broken stuff: -- I don't think online games work. Not sure why (I do copy the TitleID to the - proper spot, I think). If it works for you, let me know. Likewise, if you know - what the problem is or you can fix it, please let me know. - -Who? -==== - -Program: -Copyright 2008-2009 Hector Martin (marcan) - -Supporting code: -Copyright 2008-2009 Segher Boessenkool -Copyright 2008-2009 Andre Heider (dhewg) -Copyright 2008 Nuke - -Awesome icon: -Copyright 2009 Freddy Leitner (drmr) - -This code is licensed to you under the terms of the GNU GPL, version 2; -see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt - -The icon is licensed under CC-BY-NC-SA 3.0: -http://creativecommons.org/licenses/by-nc-sa/3.0/ - -Why? -==== - -Because: -- System Menu 4.2 forever broke region free via modchips -- I have a USA Wii which I regularly use with EUR games -- Gecko OS is somewhat annoying and recent versions are unstable (for me anyway) -- I refuse to perform retarded firmware mods. -- I autoboot HBC via BootMii-boot2 and using the System Menu takes longer - anyway. - -I also think that low-level apps that tightly hug the hardware are very -educational, so why not. And hey, the code is pretty short, so you ought to be -able to read it completely and learn how all this crazy Wii stuff *actually* -works :-) - -How? -==== - -TinyLoad is not compressed. The 4 kilobytes are raw code and data, plus the ELF -header. The original loader was fit in 4 kilobytes by avoiding bloated libraries -like libogc and instead using a small codebase pieced together from bits of the -Twilight Hack and of HBC's reload stub. Extra features (SYSCONF reading, RTC -reading, proper lowmem settings, VI stuff, progress bar, etc) were added by -making space via a combination of increasingly complex compiler options, manual -tweaks, and micro-optimization. - -Nonetheless, there is almost no assembly code and the C code, though compact and -odd at times, should be moderately readable. Just keep in mind that I -deliberately leave old values floating around and/or use odd initializations in -order to simplify the code. Also, I haven't commented most of it. If you're -really interested, give me a shout and I'll see what I can do to make it more -acceptable. - -If you want some *real* fun, load the ELF in IDA. The function inlining and tail -call goodness ought to provide tons of entertainment. - -Where? -====== - -http://wiibrew.org/TinyLoad -http://marcansoft.com/blog/ diff --git a/nand-loader/app_booter.s b/nand-loader/app_booter.s deleted file mode 100644 index 51e5066..0000000 --- a/nand-loader/app_booter.s +++ /dev/null @@ -1,62 +0,0 @@ -/* Generated by BIN2S - please don't edit directly */ - .section .rodata - .balign 4 - .global app_booter_bin_size - .global app_booter_bin -app_booter_bin: - .byte 124, 96, 0,166, 84, 99, 4, 94, 96, 99, 32, 0,124, 96, 1, 36 - .byte 76, 0, 1, 44, 60, 32,147, 1, 56, 33,131, 64, 56, 0, 0, 0 - .byte 148, 1,255,192, 60, 96,147, 0, 56, 99, 3, 32, 56,128, 0, 0 - .byte 60,160,147, 0, 56,165, 3, 32,124,163, 40, 80, 72, 0, 2,105 - .byte 72, 0, 1,212,148, 33,255,224,124, 8, 2,166,191,129, 0, 16 - .byte 124,125, 27,121,144, 1, 0, 36, 56, 96, 0, 0, 65,130, 0,152 - .byte 127,190,235,120,127,191,235,120, 59,128, 0, 0,128,191, 0,144 - .byte 47,133, 0, 0, 65,158, 0, 40,128,127, 0, 72, 43,131, 0,255 - .byte 64,189, 0, 28,128,159, 0, 0,124,157, 34, 20, 72, 0, 2, 57 - .byte 128,127, 0, 72,128,159, 0,144, 72, 0, 2, 81, 47,156, 0, 6 - .byte 59,255, 0, 4, 59,156, 0, 1, 64,158,255,196, 59,224, 0, 0 - .byte 128,190, 0,172, 47,133, 0, 0, 65,158, 0, 40,128,126, 0,100 - .byte 43,131, 0,255, 64,189, 0, 28,128,158, 0, 28,124,157, 34, 20 - .byte 72, 0, 1,245,128,126, 0,100,128,158, 0,172, 72, 0, 2, 13 - .byte 47,159, 0, 10, 59,222, 0, 4, 59,255, 0, 1, 64,158,255,196 - .byte 128,125, 0,224,128, 1, 0, 36,187,129, 0, 16, 56, 33, 0, 32 - .byte 124, 8, 3,166, 78,128, 0, 32,129, 35, 0, 0, 60, 0,127, 69 - .byte 96, 0, 76, 70,127,137, 0, 0, 57, 32, 0, 0, 64,190, 0, 36 - .byte 160, 3, 0, 16, 47,128, 0, 2, 64,158, 0, 20,160, 3, 0, 18 - .byte 57, 32, 0, 1, 47,128, 0, 20, 65,190, 0, 8, 57, 32,255,255 - .byte 125, 35, 75,120, 78,128, 0, 32,124, 8, 2,166,148, 33,255,224 - .byte 191,161, 0, 20,124,126, 27,120,144, 1, 0, 36,129, 35, 0, 28 - .byte 47,137, 0, 0, 65,158, 0,152,160, 3, 0, 44, 47,128, 0, 0 - .byte 65,158, 0,140,160, 3, 0, 42,127,227, 74, 20, 59,160, 0, 0 - .byte 47,128, 0, 32, 65,158, 0, 88, 72, 0, 0,116,128, 31, 0, 0 - .byte 47,128, 0, 1, 64,190, 0, 68,128,191, 0, 16,128, 31, 0, 20 - .byte 129, 63, 0, 12, 47, 5, 0, 0,127,133, 0, 64, 85, 41, 0,190 - .byte 101, 35,128, 0,144,127, 0, 12, 65,157, 0, 68, 65,186, 0, 28 - .byte 128,159, 0, 4,124,158, 34, 20, 72, 0, 0,253,128,127, 0, 12 - .byte 128,159, 0, 20, 72, 0, 1, 21, 59,255, 0, 32,160, 30, 0, 44 - .byte 127,157, 0, 0, 59,189, 0, 1, 65,156,255,164,128, 30, 0, 24 - .byte 84, 0, 0,190,100, 3,128, 0, 72, 0, 0, 8, 56, 96, 0, 0 - .byte 128, 1, 0, 36,187,161, 0, 20, 56, 33, 0, 32,124, 8, 3,166 - .byte 78,128, 0, 32,124, 8, 2,166,148, 33,255,224, 60, 96,145, 0 - .byte 191,161, 0, 20,144, 1, 0, 36, 75,255,254,225, 47,131, 0, 1 - .byte 64,190, 0, 16, 60, 96,145, 0, 75,255,255, 17, 72, 0, 0, 12 - .byte 60, 96,145, 0, 75,255,254, 1, 47,131, 0, 0,124,127, 27,120 - .byte 65,158, 0, 64, 61, 32,147, 32,129, 41, 0, 0,109, 32, 95, 97 - .byte 47,128,114,103, 64,158, 0, 36, 59,163, 0, 8, 60,128,147, 32 - .byte 127,163,235,120, 56,160, 0, 24, 72, 0, 0, 77,127,163,235,120 - .byte 56,128, 0, 24, 72, 0, 0,101,127,233, 3,166, 78,128, 4, 33 - .byte 128, 1, 0, 36,187,161, 0, 20, 56, 33, 0, 32,124, 8, 3,166 - .byte 78,128, 0, 32, 56,165, 0, 1, 57, 32, 0, 0,124,169, 3,166 - .byte 72, 0, 0, 12,124,137, 25,174, 57, 41, 0, 1, 66, 0,255,248 - .byte 78,128, 0, 32, 56,165, 0, 1, 57, 32, 0, 0,124,169, 3,166 - .byte 72, 0, 0, 16,124, 9, 32,174,124, 9, 25,174, 57, 41, 0, 1 - .byte 66, 0,255,244, 78,128, 0, 32, 56,132, 0, 31,124,132, 26, 20 - .byte 84, 99, 0, 52, 84,132, 0, 52, 72, 0, 0, 20,124, 0, 24,108 - .byte 124, 0, 4,172,124, 0, 31,172, 56, 99, 0, 32,127,131, 32, 64 - .byte 65,156,255,236,124, 0, 4,172, 76, 0, 1, 44, 78,128, 0, 32 - - .global app_booter_bin_end -app_booter_bin_end: - - .align -app_booter_bin_size: .int 800 diff --git a/nand-loader/binary/vWii_NAND_Loader_svn_r2.zip b/nand-loader/binary/vWii_NAND_Loader_svn_r2.zip deleted file mode 100644 index 4f5cc99..0000000 Binary files a/nand-loader/binary/vWii_NAND_Loader_svn_r2.zip and /dev/null differ diff --git a/nand-loader/cache.c b/nand-loader/cache.c deleted file mode 100644 index 85689d8..0000000 --- a/nand-loader/cache.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -/* This code comes from the Twilight Hack */ -// Copyright 2008-2009 Segher Boessenkool -// Copyright 2008-2009 Hector Martin - -#include "types.h" - -void sync_before_read(void *p, u32 len) -{ - u32 a, b; - - a = (u32)p & ~0x1f; - b = ((u32)p + len + 0x1f) & ~0x1f; - - for ( ; a < b; a += 32) - asm("dcbi 0,%0" : : "b"(a) : "memory"); - - asm("sync ; isync"); -} - -void sync_after_write(const void *p, u32 len) -{ - u32 a, b; - - a = (u32)p & ~0x1f; - b = ((u32)p + len + 0x1f) & ~0x1f; - - for ( ; a < b; a += 32) - asm("dcbf 0,%0" : : "b"(a)); - - asm("sync ; isync"); -} - diff --git a/nand-loader/cache.h b/nand-loader/cache.h deleted file mode 100644 index 9c4c8e6..0000000 --- a/nand-loader/cache.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -/* This code comes from the Twilight Hack */ -// Copyright 2008-2009 Segher Boessenkool -// Copyright 2008-2009 Hector Martin - -#ifndef __CACHE_H__ -#define __CACHE_H__ - -#include "types.h" - -void sync_before_read(void *p, u32 len); -void sync_after_write(const void *p, u32 len); - -#endif - diff --git a/nand-loader/crt0.S b/nand-loader/crt0.S deleted file mode 100644 index 530b5c9..0000000 --- a/nand-loader/crt0.S +++ /dev/null @@ -1,163 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt - -# This code comes from the Twilight Hack -# Copyright 2008-2009 Segher Boessenkool - -# Slightly modified to imitate the homebrew channel stub - -*/ -.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; -.set r5,5; .set r6,6; .set r7,7; .set r8,8; .set r9,9; -.set r10,10; .set r11,11; .set r12,12; .set r13,13; .set r14,14; -.set r15,15; .set r16,16; .set r17,17; .set r18,18; .set r19,19; -.set r20,20; .set r21,21; .set r22,22; .set r23,23; .set r24,24; -.set r25,25; .set r26,26; .set r27,27; .set r28,28; .set r29,29; -.set r30,30; .set r31,31; - -#include "hw.h" - -.extern _main -#TODO - fixup memset - .globl _start -_start: - bl __init_registers - bl __init_memory - bl __init_syscall - bl __init_sprs - lis r3,__bss_start@h - ori r3,r3,__bss_start@l - li r4,0 - lis r5,__bss_end@h - ori r5,r5,__bss_end@l - subf r5,r3,r5 - bl _memset - lis r3,__stack_end@h - ori r3,r3,__stack_end@l - li r4,0 - lis r5,__stack_top@h - ori r5,r5,__stack_top@l - subf r5,r3,r5 - bl _memset - bl _main -0: - b 0b - -__init_registers: - li r0,0 - li r3,0 - li r4,0 - li r5,0 - li r6,0 - li r7,0 - li r8,0 - li r9,0 - li r10,0 - li r11,0 - li r12,0 - li r14,0 - li r15,0 - li r16,0 - li r17,0 - li r18,0 - li r19,0 - li r20,0 - li r21,0 - li r22,0 - li r23,0 - li r24,0 - li r25,0 - li r26,0 - li r27,0 - li r28,0 - li r29,0 - li r30,0 - li r31,0 - lis r1,__stack_top@h - ori r1,r1,__stack_top@l - addi r1,r1,-4 - stw r0,0(r1) - stwu r1,-0x38(r1) - lis r2,0 - ori r2,r2,0x8000 - lis r13,0 - ori r13,r13,0x8000 - blr - -_memset: - clrlwi. r6,r5,29 - rlwinm r5,r5,30,2,31 - addi r3,r3,-4 - mtctr r5 -0: - stwu r4,4(r3) - bdnz+ 0b - cmplwi r6,0 - beq- 2f -1: - stbu r4,1(r3) - addic. r6,r6,-1 - bne+ 1b -2: - blr - -__init_memory: - mflr r0 - stw r0, 0x04(r1) - stwu r1,-0x10(r1) - stw r31,0x0C(r1) - mfspr r3,rHID0 - rlwinm r0,r3,0,16,16 #HID0[ICE] - cmplwi r0,0 - bne- 0f - bl ICacheEnable -0: - mfspr r3,rHID0 - rlwinm r0,r3,0,17,17 #HID0[DCE] - cmplwi r0,0 - bne- 1f - bl DCacheEnable -1: - mfl2cr r3 - rlwinm r0,r3,0,0,0 #L2CR[L2E] - cmplwi r0,0 - bne- 2f - bl L2_Init - bl L2_Enable -2: - lwz r0, 0x14(r1) - lwz r31,0x0C(r1) - addi r1,r1,0x10 - mtlr r0 - blr - -__init_sprs: - mflr r0 - stw r0, 0x04(r1) - stwu r1,-0x18(r1) - stw r31,0x14(r1) - stw r30,0x10(r1) - stw r29,0x0C(r1) - li r3,0 - mtmmcr0 r3 - mtmmcr1 r3 - mtpmc1 r3 - mtpmc2 r3 - mtpmc3 r3 - mtpmc4 r3 - mfspr r3,rHID0 - ori r3,r3,HID0_SPD #HID0[SPD] - mtspr rHID0,r3 - mfspr r3,rHID2 - rlwinm r3,r3,0,2,0 #HID2[WPE] - mtspr rHID2,r3 - lwz r0, 0x1C(r1) - lwz r31,0x14(r1) - lwz r30,0x10(r1) - lwz r29,0x0C(r1) - addi r1,r1,0x18 - mtlr r0 - blr diff --git a/nand-loader/data/background b/nand-loader/data/background new file mode 100644 index 0000000..7714c73 Binary files /dev/null and b/nand-loader/data/background differ diff --git a/nand-loader/data/error b/nand-loader/data/error new file mode 100644 index 0000000..15c1075 Binary files /dev/null and b/nand-loader/data/error differ diff --git a/nand-loader/data/loading b/nand-loader/data/loading new file mode 100644 index 0000000..5410cc7 Binary files /dev/null and b/nand-loader/data/loading differ diff --git a/nand-loader/hw.h b/nand-loader/hw.h deleted file mode 100644 index f4486ba..0000000 --- a/nand-loader/hw.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef __HW_H__ -#define __HW_H__ - -#define MSR_POW (1<<18) -#define MSR_ILE (1<<16) -#define MSR_EE (1<<15) -#define MSR_PR (1<<14) -#define MSR_FP (1<<13) -#define MSR_ME (1<<12) -#define MSR_FE0 (1<<11) -#define MSR_SE (1<<10) -#define MSR_BE (1<< 9) -#define MSR_FE1 (1<< 8) -#define MSR_IP (1<< 6) -#define MSR_IR (1<< 5) -#define MSR_DR (1<< 4) -#define MSR_RI (1<< 1) -#define MSR_LE (1<< 0) - -#define HID0_EMCP (1<<31) -#define HID0_DBP (1<<30) -#define HID0_EBA (1<<29) -#define HID0_EBD (1<<28) -#define HID0_BCLK (1<<27) -#define HID0_ECLK (1<<25) -#define HID0_PAR (1<<24) -#define HID0_DOZE (1<<23) -#define HID0_NAP (1<<22) -#define HID0_SLEEP (1<<21) -#define HID0_DPM (1<<20) -#define HID0_NHR (1<<16) -#define HID0_ICE (1<<15) -#define HID0_DCE (1<<14) -#define HID0_ILOCK (1<<13) -#define HID0_DLOCK (1<<12) -#define HID0_ICFI (1<<11) -#define HID0_DCFI (1<<10) -#define HID0_SPD (1<< 9) -#define HID0_IFEM (1<< 8) -#define HID0_SGE (1<< 7) -#define HID0_DCFA (1<< 6) -#define HID0_BTIC (1<< 5) -#define HID0_ABE (1<< 3) -#define HID0_BHT (1<< 2) -#define HID0_NOOPTI (1<< 0) - -#define HID2_LSQE (1<<31) -#define HID2_WPE (1<<30) -#define HID2_PSE (1<<29) -#define HID2_LCE (1<<28) - -#define L2CR_L2E (1<<31) -#define L2CR_L2CE (1<<30) -#define L2CR_L2DO (1<<22) -#define L2CR_L2I (1<<21) -#define L2CR_L2WT (1<<19) -#define L2CR_L2TS (1<<18) -#define L2CR_L2IP (1<< 0) - -#define DMAU_MEM_ADDR_MASK 0xFFFFFFE0 -#define DMAU_LENU(x) (x & 0x1F) -#define DMAL_LC_ADDR_MASK 0xFFFFFFE0 -#define DMAL_LD (1<< 4) -#define DMAL_LENL(x) (x & 0xC) -#define DMAL_T (1<< 1) -#define DMAL_F (1<< 0) - -#define BATU_BEPI_MASK 0xFFFC0000 -#define BATU_BL(x) (x & 0x00001FFC) -#define BATU_VS (1<< 1) -#define BATU_VP (1<< 0) -#define BATL_BRPN_MASK 0xFFFC0000 -#define BATL_WIMG_MASK 0x78 -#define BATL_PP (1<< 0) - -// BATU - 0x80001FFF == 256Mbytes -// 1000 0000 000x xxx0 0001 1111 1111 11xx -// 0x80000000|256Mbytes|VS|VP -// BATL - 0x00000002 -// 0000 0000 0000 000x xxxx xxxx x000 0x10 -// PP=b10 -// -// BATU - 0xC0001FFF == 256Mbytes -// BATL - 0x0000002a -// 0000 0000 0000 000x xxxx xxxx x010 1x10 -// WIMG=b0101|PP=b10 -// - -#define rHID2 920 -#define rDMAU 922 -#define rDMAL 923 -#define rHID0 1008 -#define rHID1 1009 -#define rHID4 1011 - -/* - * Upper PTE - * 0|1-24|25|26-31 - * V|VSID|H |API - * - * Lower PTE - * 0-19|20-22|23|24|25-28|29|30-31 - * RPN |000 |R |C |WIMG |0 |PP -*/ - -#endif - diff --git a/nand-loader/ios.c b/nand-loader/ios.c deleted file mode 100644 index 76f8456..0000000 --- a/nand-loader/ios.c +++ /dev/null @@ -1,286 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -/* This code comes from HBC's stub which was based on the Twilight Hack code */ -// Copyright 2008-2009 Segher Boessenkool -// Copyright 2008-2009 Andre Heider -// Copyright 2008-2009 Hector Martin - -#include "ios.h" -#include "cache.h" -#include "utils.h" - -#define virt_to_phys(x) ((u32*)(((u32)(x))&0x3FFFFFFF)) -#define phys_to_virt(x) ((u32*)(((u32)(x))|0x80000000)) - -// Low-level IPC access. - -static inline u32 -iread32(u32 addr) -{ - u32 x; - - asm volatile("lwz %0,0(%1) ; sync ; isync" : "=r"(x) : "b"(0xc0000000 | addr)); - - return x; -} - -static inline void -iwrite32(u32 addr, u32 x) -{ - asm volatile("stw %0,0(%1) ; eieio" : : "r"(x), "b"(0xc0000000 | addr)); -} - -static u32 _ipc_read(u32 reg) __attribute__((noinline)); -static void _ipc_write(u32 reg, u32 value) __attribute__((noinline)); -static void ipc_bell(u32 w) __attribute__((noinline)); - -// inline the 4*, don't inline the 0x0d0 stuff. yes, this saves a few bytes. -static u32 -_ipc_read(u32 reg) -{ - return iread32(0x0d000000 + reg); -} - -static void -_ipc_write(u32 reg, u32 value) -{ - iwrite32(0x0d000000 + reg, value); -} - -static inline u32 -ipc_read(u32 reg) -{ - return _ipc_read(4*reg); -} - -static inline void -ipc_write(u32 reg, u32 value) -{ - _ipc_write(4*reg, value); -} - -static void -ipc_bell(u32 w) -{ - ipc_write(1, w); -} - -static void ios_delay(void) __attribute__((noinline)); - -static void -ios_delay(void) -{ - usleep(500); -} - -static void -ipc_wait_ack(void) -{ - while(!(ipc_read(1) & 0x2)) - ; - ios_delay(); -} - -static void -ipc_wait_reply(void) -{ - while(!(ipc_read(1) & 0x4)) - ; - ios_delay(); -} - -static u32 -ipc_wait(void) -{ - u32 ret; - while(!((ret = ipc_read(1)) & 0x6)) - ; - ios_delay(); - return ret; -} - -// Mid-level IPC access. - -struct ipc { - u32 cmd; - int result; - int fd; - u32 arg[5]; - - u32 user[8]; -}; - -static struct ipc ipc ALIGNED(64); - -static void -ipc_send_request(void) -{ - sync_after_write(&ipc, 0x40); - - ipc_write(0, (u32)virt_to_phys(&ipc)); - ipc_bell(1); - - ipc_wait_ack(); - - ipc_bell(2); -} - -static int -ipc_send_twoack(void) -{ - sync_after_write(&ipc, 0x40); - ios_delay(); - - ipc_write(0, (u32)virt_to_phys(&ipc)); - ipc_bell(1); - - if(ipc_wait() & 4) - return 0; - - ipc_bell(2); - - if(ipc_wait() & 4) - return 0; - - ipc_bell(2); - ipc_bell(8); - return 1; -} - -static void -ipc_recv_reply(void) -{ - for (;;) - { - u32 reply; - - ipc_wait_reply(); - - reply = ipc_read(2); - ipc_bell(4); - - ipc_bell(8); - - if (((u32*)reply) == virt_to_phys(&ipc)) - break; - } - - sync_before_read(&ipc, 0x40); -} - - -// High-level IPC access. - -int -ios_open(const char *filename, u32 mode) -{ - sync_after_write((void*)filename, 0x20); - - ipc.cmd = 1; - ipc.fd = 0; - ipc.arg[0] = (u32)virt_to_phys(filename); - ipc.arg[1] = mode; - - ipc_send_request(); - ipc_recv_reply(); - - return ipc.result; -} - -int -ios_close(int fd) -{ - ipc.cmd = 2; - ipc.fd = fd; - - ipc_send_request(); - ipc_recv_reply(); - - return ipc.result; -} - -static void -ios_std(int fd, int cmd) -{ - ipc.cmd = cmd; - ipc.fd = fd; - - ipc_send_request(); - ipc_recv_reply(); -} - -int -ios_read(int fd, void *buf, u32 size) -{ - ipc.arg[0] = (u32)virt_to_phys(buf); - ipc.arg[1] = size; - - ios_std(fd, 3); - - sync_before_read(buf, size); - - return ipc.result; -} - -int -_ios_ioctlv(int fd, u32 n, u32 in_count, u32 out_count, struct ioctlv *vec, int reboot) -{ - u32 i; - - for (i = 0; i < in_count + out_count; i++) - { - if (vec[i].data) - { - sync_after_write(vec[i].data, vec[i].len); - vec[i].data = (void *)virt_to_phys(vec[i].data); - } - } - - sync_after_write(vec, (in_count + out_count) * sizeof *vec); - - ipc.cmd = 7; - ipc.fd = fd; - ipc.arg[0] = n; - ipc.arg[1] = in_count; - ipc.arg[2] = out_count; - ipc.arg[3] = (u32)virt_to_phys(vec); - - if(reboot) - { - if(ipc_send_twoack()) - return 0; - } - else - ipc_send_request(); - ipc_recv_reply(); - - for(i = in_count; i < in_count + out_count; i++) - { - if (vec[i].data) - { - vec[i].data = phys_to_virt((u32)vec[i].data); - sync_before_read(vec[i].data, vec[i].len); - } - } - if(reboot && (ipc.result >= 0)) - return -100; - return ipc.result; -} - -int -ios_ioctlv(int fd, u32 n, u32 in_count, u32 out_count, struct ioctlv *vec) -{ - return _ios_ioctlv(fd, n, in_count, out_count, vec, 0); -} - -int -ios_ioctlvreboot(int fd, u32 n, u32 in_count, u32 out_count, struct ioctlv *vec) -{ - return _ios_ioctlv(fd, n, in_count, out_count, vec, 1); -} - diff --git a/nand-loader/ios.h b/nand-loader/ios.h deleted file mode 100644 index 9aab418..0000000 --- a/nand-loader/ios.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -#ifndef __IOS_H__ -#define __IOS_H__ - -// Copyright 2008-2009 Hector Martin - -#include "types.h" - -struct ioctlv { - void *data; - u32 len; -}; - -int ios_open(const char *filename, u32 mode); -int ios_close(int fd); -int ios_ioctlv(int fd, u32 n, u32 in_count, u32 out_count, struct ioctlv *vec); -int ios_ioctlvreboot(int fd, u32 n, u32 in_count, u32 out_count, struct ioctlv *vec); -int ios_read(int fd, void *buf, u32 size); - -#endif - diff --git a/nand-loader/main.c b/nand-loader/main.c deleted file mode 100644 index 9fda6be..0000000 --- a/nand-loader/main.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -/* This code comes from HBC's stub which was based on dhewg's geckoloader stub */ -// Copyright 2008-2009 Andre Heider -// Copyright 2008-2009 Hector Martin - -#include "utils.h" -#include "ios.h" -#include "cache.h" -#include "memory.h" - -#define IOCTL_ES_LAUNCH 0x08 -#define IOCTL_ES_OPENCONTENT 0x09 -#define IOCTL_ES_READCONTENT 0x0A -#define IOCTL_ES_CLOSECONTENT 0x0B -#define IOCTL_ES_GETVIEWCNT 0x12 -#define IOCTL_ES_GETVIEWS 0x13 -#define IOCTL_ES_GETTITLEID 0x20 -#define IOCTL_ES_SEEKCONTENT 0x23 - -#define TITLE_ID(x,y) (((u64)(x) << 32) | (y)) - -extern const u8 app_booter_bin[]; -extern const u32 app_booter_bin_size; - -static struct ioctlv vecs[16] ALIGNED(64); -s32 cfd ALIGNED(32); -int es_fd = -1; - -typedef void (*entry)(void); - -static int es_init(void) -{ - es_fd = ios_open("/dev/es", 0); - return es_fd; -} - -static void es_close(void) -{ - if(es_fd >= 0) - ios_close(es_fd); - es_fd = -1; -} - -static int es_launchtitle(u64 titleID) -{ - static u64 xtitleID __attribute__((aligned(32))); - static u32 cntviews __attribute__((aligned(32))); - static u8 tikviews[0xd8*4] __attribute__((aligned(32))); - - xtitleID = titleID; - vecs[0].data = &xtitleID; - vecs[0].len = 8; - vecs[1].data = &cntviews; - vecs[1].len = 4; - int ret = ios_ioctlv(es_fd, IOCTL_ES_GETVIEWCNT, 1, 1, vecs); - if(ret<0) return ret; - if(cntviews>4) return -1; - - vecs[2].data = tikviews; - vecs[2].len = 0xd8*cntviews; - ret = ios_ioctlv(es_fd, IOCTL_ES_GETVIEWS, 2, 1, vecs); - if(ret<0) return ret; - vecs[1].data = tikviews; - vecs[1].len = 0xd8; - ret = ios_ioctlvreboot(es_fd, IOCTL_ES_LAUNCH, 2, 0, vecs); - return ret; -} - -static int es_opencontent(u32 content_num) -{ - vecs[0].data = &content_num; - vecs[0].len = 4; - cfd = ios_ioctlv(es_fd, IOCTL_ES_OPENCONTENT, 1, 0, vecs); - return cfd; -} - -static int es_seekcontent(s32 place, s32 from) -{ - vecs[0].data = &cfd; - vecs[0].len = 4; - vecs[1].data = &place; - vecs[1].len = 4; - vecs[2].data = &from; - vecs[2].len = 4; - return ios_ioctlv(es_fd, IOCTL_ES_SEEKCONTENT, 3, 0, vecs); -} - -static int es_readcontent(void *data, u32 size) -{ - vecs[0].data = &cfd; - vecs[0].len = 4; - vecs[1].data = data; - vecs[1].len = size; - return ios_ioctlv(es_fd, IOCTL_ES_READCONTENT, 1, 1, vecs); -} - -static int es_closecontent() -{ - vecs[0].data = &cfd; - vecs[0].len = 4; - return ios_ioctlv(es_fd, IOCTL_ES_CLOSECONTENT, 1, 0, vecs); -} - -void memoryset(u8 *dst, const u8 value, u32 size) -{ - u32 i; - for(i = 0; i < size; ++i) - dst[i] = value; -} - -void memorycopy(u8 *dst, const u8 *src, u32 size) -{ - u32 i; - for(i = 0; i < size; ++i) - dst[i] = src[i]; -} - -static void *dol_data = (void*)0x91000000; /* 32MB Max */ -static void *appbooter_entry = (void*)0x93000000; - -void _main(void) -{ - u32 size = 0; - es_init(); - /* load up dol */ - es_opencontent(2); //stub is 1, real dol is 2 - if(cfd < 0) goto fail; - size = es_seekcontent(0, 2); - es_seekcontent(0, 0); - es_readcontent(dol_data, size); - sync_after_write(dol_data, size); - es_closecontent(); - if(size == 0) goto fail; - es_close(); - /* Boot our good old appbooter stuff */ - memorycopy(appbooter_entry, app_booter_bin, app_booter_bin_size); - sync_after_write(appbooter_entry, app_booter_bin_size); - entry EntryPoint = (entry)appbooter_entry; - EntryPoint(); - -fail: - es_launchtitle(TITLE_ID(1,2)); - while(1) usleep(500); -} - -#define SYSCALL_VECTOR ((u8*)0x80000C00) -void __init_syscall() -{ - u8* sc_vector = SYSCALL_VECTOR; - u32 bytes = (u32)DCFlashInvalidate - (u32)__temp_abe; - u8* from = (u8*)__temp_abe; - for ( ; bytes != 0 ; --bytes ) - { - *sc_vector = *from; - sc_vector++; - from++; - } - - sync_after_write(SYSCALL_VECTOR, 0x100); - ICInvalidateRange(SYSCALL_VECTOR, 0x100); -} diff --git a/nand-loader/make_app/main.cpp b/nand-loader/make_app/main.cpp deleted file mode 100644 index 8d9e513..0000000 --- a/nand-loader/make_app/main.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************************** - * Copyright (C) 2012 FIX94 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - ****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include - -unsigned char dol_header[0x100]; -#define ALIGN32(x) (((x) + 31) & ~31) - -int main() -{ - char nullBuf[1]; - nullBuf[0] = 0x00; - memset(&dol_header, 0, 0x100); - /* Read in shit */ - FILE *f = fopen("loader.bin", "rb"); - if(f == NULL) - { - printf("no loader\n"); - return -1; - } - fseek(f, 0, SEEK_END); - unsigned int size = ftell(f); - rewind(f); - unsigned char *buf = (unsigned char*)malloc(size); - fread(buf, size, 1, f); - fclose(f); - f = fopen("stub.bin", "rb"); - if(f == NULL) - { - printf("no stub\n"); - free(buf); - return -2; - } - fseek(f, 0, SEEK_END); - unsigned int stub_size = ftell(f); - rewind(f); - unsigned char *stub_buf = (unsigned char*)malloc(stub_size); - fread(stub_buf, stub_size, 1, f); - fclose(f); - /* Add stub text */ - ((unsigned int*)dol_header)[0] = _bswap(0x100); /* Header size */ - ((unsigned int*)dol_header)[18] = _bswap(0x80003400); - ((unsigned int*)dol_header)[36] = _bswap(ALIGN32(stub_size)); - /* Add NAND Loader text */ - ((unsigned int*)dol_header)[1] = _bswap(0x100 + ALIGN32(stub_size)); - ((unsigned int*)dol_header)[19] = _bswap(0x80004000); - ((unsigned int*)dol_header)[37] = _bswap(ALIGN32(size)); - /* Set our entry to the NAND Loader */ - ((unsigned int*)dol_header)[56] = _bswap(0x80004000); - /* Write it to a new file */ - f = fopen("00000001.app", "wb"); - fwrite(dol_header, 0x100, 1, f); /* Write header first */ - - fwrite(stub_buf, stub_size, 1, f); /* Then stub */ - if(ALIGN32(stub_size) > stub_size) - { - for(int i = 0; i < (ALIGN32(stub_size) - stub_size); ++i) - fwrite(nullBuf, 1, 1, f); - } - - fwrite(buf, size, 1, f); /* and last nand loader */ - if(ALIGN32(size) > size) - { - for(int i = 0; i < (ALIGN32(size) - size); ++i) - fwrite(nullBuf, 1, 1, f); - } - - fclose(f); - free(buf); - free(stub_buf); - printf("done\n"); - return 0; -} diff --git a/nand-loader/make_app/make_app.exe b/nand-loader/make_app/make_app.exe deleted file mode 100644 index ca97b08..0000000 Binary files a/nand-loader/make_app/make_app.exe and /dev/null differ diff --git a/nand-loader/memory.S b/nand-loader/memory.S deleted file mode 100644 index 1cb1c91..0000000 --- a/nand-loader/memory.S +++ /dev/null @@ -1,192 +0,0 @@ -.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; -.set r5,5; .set r6,6; .set r7,7; .set r8,8; .set r9,9; -.set r10,10; .set r11,11; .set r12,12; .set r13,13; .set r14,14; -.set r15,15; .set r16,16; .set r17,17; .set r18,18; .set r19,19; -.set r20,20; .set r21,21; .set r22,22; .set r23,23; .set r24,24; -.set r25,25; .set r26,26; .set r27,27; .set r28,28; .set r29,29; -.set r30,30; .set r31,31; - -#include "hw.h" - -.global __temp_abe -__temp_abe: - mfspr r3,rHID0 - ori r4,r3,HID0_ABE #HID0[ABE] - mtspr rHID0,r4 - isync - sync - mtspr rHID0,r3 - rfi - -.global DCFlashInvalidate -DCFlashInvalidate: - nop - mfspr r3,rHID0 - ori r3,r3,HID0_DCFI #HID0[DCFI] - mtspr rHID0,r3 - blr - -.global DCacheEnable -DCacheEnable: - sync - mfspr r3,rHID0 - ori r3,r3,HID0_DCE #HID0[DCE] - mtspr rHID0,r3 - blr - -.global DCBlockInvalidate -DCBlockInvalidate: - cmplwi r4,0 - blelr - clrlwi. r5,r3,27 - beq- 0f - addi r4,r4,0x20 -0: - addi r4,r4,0x1F - rlwinm r4,r4,27,5,31 - mtctr r4 -1: - dcbi r0,r3 - addi r3,r3,0x20 - bdnz+ 1b - blr - -DCBlockFlushSc: - cmplwi r4,0 - blelr - clrlwi. r5,r3,27 - beq- 0f - addi r4,r4,0x20 -0: - addi r4,r4,0x1F - rlwinm r4,r4,27,5,31 - mtctr r4 -1: - dcbf r0,r3 - addi r3,r3,0x20 - bdnz+ 1b - sc - blr - -DCBlockFlush: - cmplwi r4,0 - blelr - clrlwi. r5,r3,27 - beq- 0f - addi r4,r4,0x20 -0: - addi r4,r4,0x1F - rlwinm r4,r4,27,5,31 - mtctr r4 -1: - dcbf r0,r3 - addi r3,r3,0x20 - bdnz+ 1b - blr - -.global ICInvalidateRange -ICInvalidateRange: - cmplwi r4,0 - blelr - clrlwi. r5,r3,27 - beq- 0f - addi r4,r4,0x20 -0: - addi r4,r4,0x1F - rlwinm r4,r4,27,5,31 - mtctr r4 -1: - icbi r0,r3 - addi r3,r3,0x20 - bdnz+ 1b - sync - isync - blr - -ICFlashInvalidate: - mfspr r3,rHID0 - ori r3,r3,HID0_ICFI #HID0[ICFI] - mtspr rHID0,r3 - blr - -.global ICacheEnable -ICacheEnable: - isync - mfspr r3,rHID0 - ori r3,r3,HID0_ICE #HID0[ICE] - mtspr rHID0,r3 - blr - -ICacheDisable: - isync - mfspr r3,rHID0 - rlwinm r3,r3,0,17,15 #HID0[ICE] - mtspr rHID0,r3 - blr - -ISync: - isync - blr - -.global L2_Init -L2_Init: - mflr r0 - stw r0, 0x04(r1) - stwu r1,-0x10(r1) - stw r31,0x0C(r1) - mfmsr r3 - mr r31,r3 - sync - li r3,MSR_IR|MSR_DR #MSR[IR|DR] - mtmsr r3 - sync - bl L2_Disable - bl L2_Invalidate - mr r3,r31 - mtmsr r3 - lwz r0, 0x14(r1) - lwz r31,0x0C(r1) - mtlr r0 - blr - -.global L2_Enable -L2_Enable: - mfl2cr r3 - oris r0,r3,0x8000 #L2CR[L2E] - rlwinm r3,r0,0,11,9 #L2CR[L2I] - mtl2cr r3 - blr - -L2_Disable: - sync - mfl2cr r3 - clrlwi r3,r3,1 #L2CR[L2E] - mtl2cr r3 - sync - blr - -L2_Invalidate: - mflr r0 - stw r0, 0x04(r1) - stwu r1,-0x08(r1) - bl L2_Disable - mfl2cr r3 - oris r3,r3,0x200 #L2CR[L2I] - mtl2cr r3 -0: - mfl2cr r3 - clrlwi r0,r3,31 #L2CR[L2IP] - cmplwi r0,0 - bne+ 0b - mfl2cr r3 - rlwinm r3,r3,0,11,9 #L2CR[L2I] - mtl2cr r3 -1: - mfl2cr r3 - clrlwi r0,r3,31 #L2CR[L2IP] - cmplwi r0,0 - bne+ 1b - lwz r0,0x0C(r1) - addi r1,r1,0x08 - mtlr r0 - blr diff --git a/nand-loader/memory.h b/nand-loader/memory.h deleted file mode 100644 index 03c514d..0000000 --- a/nand-loader/memory.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef __MEMORY_H__ -#define __MEMORY_H__ - -void __temp_abe(void); -void ICInvalidateRange(void *, int); -void DCFlashInvalidate(void *, int); - -#endif - diff --git a/nand-loader/openstub.ld b/nand-loader/openstub.ld deleted file mode 100644 index 8991605..0000000 --- a/nand-loader/openstub.ld +++ /dev/null @@ -1,35 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -OUTPUT_FORMAT("elf32-powerpc") -OUTPUT_ARCH(powerpc:common) - -ENTRY(_start) - -PHDRS { - app PT_LOAD FLAGS(7); -} - -SECTIONS { - . = 0x80004000; - - .text : { *(.text) *(.text.*) } :app - - .data : { *(.data) *(.data.*) } - .rodata : { *(.rodata) *(.rodata.*) } - - __bss_start = .; - .bss : { *(.bss) *(.bss.*) } - __bss_end = .; - - . = ALIGN(64); - __stack_end = .; - .stack : { - . += 0x8000; - __stack_top = .; - } -} diff --git a/nand-loader/rvl.ld b/nand-loader/rvl.ld new file mode 100644 index 0000000..9df04f4 --- /dev/null +++ b/nand-loader/rvl.ld @@ -0,0 +1,345 @@ +/* + * Linkscript for Wii + */ + +OUTPUT_FORMAT("elf32-powerpc", "elf32-powerpc", "elf32-powerpc"); +OUTPUT_ARCH(powerpc:common); +EXTERN(_stub_start); +ENTRY(_stub_start); + +SECTIONS +{ + /* DOL header (from TITANIK's GC docs) */ + /* default base address */ + off = 0; + + .header : AT(off) + { + /* 0000-001B Text[0..6] sections File Positions */ + LONG(stub_file_start); + LONG(text_file_start); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 001C-0047 Data[0..10] sections File Positions */ + LONG(data_file_start); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 0048-0063 Text[0..6] sections Mem Address */ + LONG(stub_mem_start); + LONG(text_mem_start); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 0064-008F Data[0..10] sections Mem Address */ + LONG(data_mem_start); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 0090-00AB Text[0..6] sections Sizes */ + LONG(stub_mem_size); + LONG(text_mem_size); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 00AC-00D7 Data[0..10] sections Sizes */ + LONG(data_mem_size); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + LONG(0); + + /* 00D8 BSS Mem address + 00DC BSS Size */ + LONG(bss_mem_start); + LONG(bss_mem_size); + + /* 00E0 Entry Point */ + LONG(ABSOLUTE(_stub_start)); + } + + . = 0x00003400; + off += 0x100; + + stub_mem_start = .; + stub_file_start = off; + + .stub : AT(off) + { + KEEP (*(.stub)) + . = 0x400; + } = 0 + + stub_mem_size = . - stub_mem_start; + off += stub_mem_size; + + . = 0x81330000; + + text_mem_start = .; + text_file_start = off; + + .init : AT(off) + { + KEEP (*crt0.o(*.init)) + KEEP (*(.init)) + } + + .text : + { + *(.text) + *(.text.*) + /* .gnu.warning sections are handled specially by elf32.em. */ + *(.gnu.warning) + *(.gnu.linkonce.t.*) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + .fini : + { + KEEP (*(.fini)) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + PROVIDE (__etext = .); + PROVIDE (_etext = .); + PROVIDE (etext = .); + + text_mem_size = . - text_mem_start; + off += text_mem_size; + + data_mem_start = .; + data_file_start = off; + + .data : AT(off) + { + *(.rodata) *(.rodata.*) *(.gnu.linkonce.r.*) + *(.rodata1) + *(.sdata2) *(.sdata2.*) *(.gnu.linkonce.s2.*) + *(.sbss2) *(.sbss2.*) *(.gnu.linkonce.sb2.*) + PROVIDE (__preinit_array_start = .); + *(.preinit_array) + PROVIDE (__preinit_array_end = .); + PROVIDE (__init_array_start = .); + *(.init_array) + PROVIDE (__init_array_end = .); + PROVIDE (__fini_array_start = .); + *(.fini_array) + PROVIDE (__fini_array_end = .); + + *(.data) + *(.data.*) + *(.gnu.linkonce.d.*) + SORT(CONSTRUCTORS) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + .data1 : { *(.data1) } + .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) } + .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } + .gcc_except_table : { *(.gcc_except_table) } + .fixup : { *(.fixup) } + .got1 : { *(.got1) } + .got2 : { *(.got2) } + .dynamic : { *(.dynamic) } + + .ctors : + { + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. */ + + KEEP (*crtbegin.o(.ctors)) + + /* We don't want to include the .ctor section from + from the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last */ + + KEEP (*(EXCLUDE_FILE (*crtend.o ) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + .dtors : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o ) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + .jcr : { KEEP (*(.jcr)) } + .got : { *(.got.plt) *(.got) } + + + /* We want the small data sections together, so single-instruction offsets + can access them all, and initialized data all before uninitialized, so + we can shorten the on-disk segment size. */ + + .sdata : + { + *(.sdata) + *(.sdata.*) + *(.gnu.linkonce.s.*) + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + } + + _edata = .; + PROVIDE (edata = .); + + data_mem_size = . - data_mem_start; + off += data_mem_size; + + bss_mem_start = .; + bss_file_start = off; + + .sbss : AT(off) + { + __sbss_start = .; + PROVIDE (__sbss_start = .); + PROVIDE (___sbss_start = .); + *(.dynsbss) + *(.sbss) + *(.sbss.*) + *(.gnu.linkonce.sb.*) + *(.scommon) + PROVIDE (__sbss_end = .); + PROVIDE (___sbss_end = .); + . = ALIGN(32); /* REQUIRED. LD is flaky without it. */ + __sbss_end = .; + } + + .bss : + { + __bss_start = .; + PROVIDE (__bss_start = .); + *(.dynbss) + *(.bss) + *(.bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + /* Align here to ensure that the .bss section occupies space up to + _end. Align after .bss to ensure correct alignment even if the + .bss section disappears because there are no input sections. */ + + . = ALIGN(32); + + PROVIDE (__bss_end = .); + __bss_end = .; + } + + bss_mem_size = . - bss_mem_start; + + _end = .; + PROVIDE(end = .); + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + /* These must appear regardless of . */ +} + +__isIPL = 0; +__stack_addr = (__bss_start + SIZEOF(.bss) + 0x20000 + 7) & (-8); +__stack_end = (__bss_start + SIZEOF(.bss)); +__intrstack_addr = (__stack_addr + 0x4000); +__intrstack_end = (__stack_addr); +__Arena1Lo = (__intrstack_addr + 31) & (-32); +__Arena1Hi = (0x816ffff0); +__Arena2Lo = (0x90002000); +__Arena2Hi = (0x933E0000); + +__gxregs = (__Arena1Hi + 31) & (-32); +__ipcbufferLo = (0x933e0000); +__ipcbufferHi = (0x93400000); + +/* for backward compatibility with old crt0 */ +PROVIDE (__stack = (0x816ffff0)); + +PROVIDE(__isIPL = __isIPL); +PROVIDE(__text_start = text_mem_start); +PROVIDE(__text_fstart = text_file_start); +PROVIDE(__data_start = data_mem_start); +PROVIDE(__data_fstart = data_file_start); +PROVIDE(__bss_start = bss_mem_start); +PROVIDE(__bss_fstart = bss_file_start); +PROVIDE(__stack_addr = __stack_addr); +PROVIDE(__stack_end = __stack_end); +PROVIDE(__intrstack_addr = __intrstack_addr); +PROVIDE(__intrstack_end = __intrstack_end); +PROVIDE(__Arena1Lo = __Arena1Lo); +PROVIDE(__Arena1Hi = __Arena1Hi); +PROVIDE(__Arena2Lo = __Arena2Lo); +PROVIDE(__Arena2Hi = __Arena2Hi); +PROVIDE(__ipcbufferLo = __ipcbufferLo); +PROVIDE(__ipcbufferHi = __ipcbufferHi); +PROVIDE(__gxregs = __gxregs); diff --git a/nand-loader/source/asm.h b/nand-loader/source/asm.h new file mode 100644 index 0000000..610d9c4 --- /dev/null +++ b/nand-loader/source/asm.h @@ -0,0 +1,93 @@ +#ifndef _ASM_H_ +#define _ASM_H_ + +/* Registers */ +#define r0 0 +#define r1 1 +#define r2 2 +#define r3 3 +#define r4 4 +#define r5 5 +#define r6 6 +#define r7 7 +#define r8 8 +#define r9 9 +#define r10 10 +#define r11 11 +#define r12 12 +#define r13 13 +#define r14 14 +#define r15 15 +#define r16 16 +#define r17 17 +#define r18 18 +#define r19 19 +#define r20 20 +#define r21 21 +#define r22 22 +#define r23 23 +#define r24 24 +#define r25 25 +#define r26 26 +#define r27 27 +#define r28 28 +#define r29 29 +#define r30 30 +#define r31 31 + + +/* BATs */ +#define IBAT0U 528 +#define IBAT0L 529 +#define IBAT1U 530 +#define IBAT1L 531 +#define IBAT2U 532 +#define IBAT2L 533 +#define IBAT3U 534 +#define IBAT3L 535 +#define IBAT4U 560 +#define IBAT4L 561 +#define IBAT5U 562 +#define IBAT5L 563 +#define IBAT6U 564 +#define IBAT6L 565 +#define IBAT7U 566 +#define IBAT7L 567 + +#define DBAT0U 536 +#define DBAT0L 537 +#define DBAT1U 538 +#define DBAT1L 539 +#define DBAT2U 540 +#define DBAT2L 541 +#define DBAT3U 542 +#define DBAT3L 543 +#define DBAT4U 568 +#define DBAT4L 569 +#define DBAT5U 570 +#define DBAT5L 571 +#define DBAT6U 572 +#define DBAT6L 573 +#define DBAT7U 574 +#define DBAT7L 575 + + +/* HIDs */ +#define HID0 1008 +#define HID1 1009 +#define HID2 920 +#define HID4 1011 + + +/* MSRs */ +#define MSR_RI 0x00000002 +#define MSR_DR 0x00000010 +#define MSR_IR 0x00000020 +#define MSR_IP 0x00000040 +#define MSR_SE 0x00000400 +#define MSR_ME 0x00001000 +#define MSR_FP 0x00002000 +#define MSR_POW 0x00004000 +#define MSR_EE 0x00008000 + +#endif diff --git a/nand-loader/source/config.c b/nand-loader/source/config.c new file mode 100644 index 0000000..a2497ce --- /dev/null +++ b/nand-loader/source/config.c @@ -0,0 +1,220 @@ +#include +#include +#include +#include + +#include "config.h" +#include "title.h" +#include "video.h" +#include "wpad.h" + +/* 'Menu' structure */ +struct menu { + /* Entry name */ + char *name; + + /* Entry value */ + bool *value; +}; + + +/* NANDLoader configuration */ +struct config loaderCfg ATTRIBUTE_ALIGN(32) = { 0 }; + +/* Config entries */ +struct menu entryList[] = { + { "Force PAL50", &loaderCfg.forcePal50 }, + { "Force PAL60", &loaderCfg.forcePal60 }, + { "Force NTSC", &loaderCfg.forceNtsc }, + { "Force HDTV", &loaderCfg.forceHdtv }, + { "Patch Video Mode", &loaderCfg.patchVmode }, + { "Show Splash", &loaderCfg.showSplash }, +}; + +/* Macros */ +#define NB_ENTRIES (sizeof(entryList) / sizeof(struct menu)) + + +void __Config_Credits(void) +{ + /* Clear console */ + Con_Clear(); + + /* Write credits */ + printf("[+] Custom NAND Loader for Wii\n\n"); + + printf(" developed by Waninkoko\n"); + printf(" www.teknoconsolas.es\n\n\n"); + + printf("[+] Thanks to all my betatesters:\n"); + printf(" - linkinworm\n"); + printf(" - hectorscasa\n"); + printf(" - SoraK05\n"); + printf(" - And the rest... ;-)\n\n"); + + printf("[+] Also kudos to:\n\n"); + printf(" - Team Twiizers\n"); + printf(" - SoftMii Team\n"); + printf(" - crediar\n"); + printf(" - WiiGator and kwiirk\n"); + printf(" - Teknoconsolas.es community\n\n"); + + printf("\n"); + printf("[+] Press any button to continue...\n"); + + /* Wait for any button */ + Wpad_WaitButtons(); +} + +s32 __Config_GetFilePath(char *outbuf) +{ + static char nandpath[ISFS_MAXPATH] ATTRIBUTE_ALIGN(32); + + u64 tid; + s32 ret; + + /* Get title ID */ + ret = ES_GetTitleID(&tid); + if (ret < 0) + return ret; + + /* Get data directory */ + ret = ES_GetDataDir(tid, nandpath); + if (ret < 0) + return ret; + + /* Generate filepath */ + sprintf(outbuf, "%s/nand.cfg", nandpath); + + return 0; +} + + +s32 Config_Read(void) +{ + char filepath[ISFS_MAXPATH]; + s32 fd, ret; + + /* Get config filepath */ + ret = __Config_GetFilePath(filepath); + if (ret < 0) + return ret; + + /* Open config file */ + fd = IOS_Open(filepath, ISFS_OPEN_READ); + if (fd < 0) + return fd; + + /* Read config */ + ret = IOS_Read(fd, &loaderCfg, sizeof(loaderCfg)); + + /* Close config file */ + IOS_Close(fd); + + return ret; +} + +s32 Config_Write(void) +{ + char filepath[ISFS_MAXPATH]; + s32 fd, ret; + + /* Get config filepath */ + ret = __Config_GetFilePath(filepath); + if (ret < 0) + return ret; + + /* Create config file */ + ISFS_CreateFile(filepath, 0, ISFS_OPEN_RW, ISFS_OPEN_READ, ISFS_OPEN_READ); + + /* Open config file */ + fd = IOS_Open(filepath, ISFS_OPEN_WRITE); + if (fd < 0) + return fd; + + /* Write config */ + ret = IOS_Write(fd, &loaderCfg, sizeof(loaderCfg)); + + /* Close config file */ + IOS_Close(fd); + + return ret; +} + +void Config_Menu(void) +{ + u32 var = 0; + s32 selected = 0; + + for (;;) { + u32 cnt; + + /* Clear console */ + Con_Clear(); + + for (cnt = 0; cnt < NB_ENTRIES; cnt++) { + struct menu *entry = &entryList[cnt]; + + /* Print entry */ + printf("\t%2s %-16s : %s\n", (selected == cnt) ? ">>" : " ", entry->name, (*entry->value) ? "Yes" : "No"); + } + + printf("\n"); + + printf("[+] Press UP/DOWN to move cursor.\n"); + printf(" Press LEFT/RIGHT to toggle option.\n\n"); + + printf(" Press A button to save and continue...\n"); + + u32 buttons = Wpad_WaitButtons(); + + /* UP/DOWN buttons */ + if (buttons & WPAD_BUTTON_UP) { + if ((--selected) <= -1) + selected = (NB_ENTRIES - 1); + } + if (buttons & WPAD_BUTTON_DOWN) { + if ((++selected) >= NB_ENTRIES) + selected = 0; + } + + /* LEFT/RIGHT buttons */ + if (buttons & (WPAD_BUTTON_LEFT | WPAD_BUTTON_RIGHT)) + *entryList[selected].value ^= 1; + + /* A button */ + if (buttons & WPAD_BUTTON_A) + break; + + + /* Easter Egg */ + if (buttons & WPAD_BUTTON_1) { + switch (var) { + case 0: + var++; + break; + case 3: + /* Show credits screen */ + __Config_Credits(); + + var = 0; + break; + default: + var = 0; + } + } + if (buttons & WPAD_BUTTON_2) { + switch (var) { + case 1: + case 2: + var++; + break; + default: + var = 0; + } + } + } + + /* Write configuration */ + Config_Write(); +} diff --git a/nand-loader/source/config.h b/nand-loader/source/config.h new file mode 100644 index 0000000..35d4be9 --- /dev/null +++ b/nand-loader/source/config.h @@ -0,0 +1,28 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +/* 'Config' structure */ +struct config { + /* Video modes */ + bool forcePal50; + bool forcePal60; + bool forceNtsc; + bool forceHdtv; + + /* Video mode patching */ + bool patchVmode; + + /* Splash screen */ + bool showSplash; +}; + +/* NANDLoader configuration */ +extern struct config loaderCfg; + +/* Prototypes */ +s32 Config_Read(void); +s32 Config_Write(void); +void Config_Menu(void); + +#endif + diff --git a/nand-loader/source/gui.c b/nand-loader/source/gui.c new file mode 100644 index 0000000..e811542 --- /dev/null +++ b/nand-loader/source/gui.c @@ -0,0 +1,87 @@ +#include +#include +#include + +#include "video.h" + +/* Constants */ +#define CONSOLE_XCOORD 20 +#define CONSOLE_YCOORD 100 +#define CONSOLE_WIDTH 350 +#define CONSOLE_HEIGHT 300 + +#define MSG_XCOORD 180 +#define MSG_YCOORD 190 + + +s32 __Gui_DrawPng(void *img, u32 x, u32 y) +{ + IMGCTX ctx = NULL; + PNGUPROP imgProp; + + s32 ret; + + /* Select PNG data */ + ctx = PNGU_SelectImageFromBuffer(img); + if (!ctx) { + ret = -1; + goto out; + } + + /* Get image properties */ + ret = PNGU_GetImageProperties(ctx, &imgProp); + if (ret != PNGU_OK) { + ret = -1; + goto out; + } + + /* Draw image */ + Video_DrawPng(ctx, imgProp, x, y); + + /* Success */ + ret = 0; + +out: + /* Free memory */ + if (ctx) + PNGU_ReleaseImageContext(ctx); + + return ret; +} + + +void Gui_InitConsole(void) +{ + /* Initialize console */ + Con_Init(CONSOLE_XCOORD, CONSOLE_YCOORD, CONSOLE_WIDTH, CONSOLE_HEIGHT); +} + +void Gui_DrawBackground(void) +{ + extern char bgData[]; + + /* Draw background */ + __Gui_DrawPng(bgData, 0, 0); +} + +void Gui_DrawLoading(void) +{ + extern char loadingImg[]; + + /* Clear screen */ + Video_Clear(COLOR_BLACK); + + /* Draw loading image */ + __Gui_DrawPng(loadingImg, MSG_XCOORD, MSG_YCOORD); +} + +void Gui_DrawError(void) +{ + extern char errorImg[]; + + /* Clear screen */ + Video_Clear(COLOR_BLACK); + + /* Draw error image */ + __Gui_DrawPng(errorImg, MSG_XCOORD, MSG_YCOORD); +} diff --git a/nand-loader/source/gui.h b/nand-loader/source/gui.h new file mode 100644 index 0000000..f4ea218 --- /dev/null +++ b/nand-loader/source/gui.h @@ -0,0 +1,10 @@ +#ifndef _GUI_H_ +#define _GUI_H_ + +/* Prototypes */ +void Gui_InitConsole(void); +void Gui_DrawBackground(void); +void Gui_DrawLoading(void); +void Gui_DrawError(void); + +#endif diff --git a/nand-loader/source/loader.c b/nand-loader/source/loader.c new file mode 100644 index 0000000..b432e94 --- /dev/null +++ b/nand-loader/source/loader.c @@ -0,0 +1,265 @@ +#include +#include +#include +#include + +#include "config.h" +#include "patches.h" +#include "video.h" +#include "wpad.h" +#include "tools.h" + +/* DOL header structure */ +typedef struct { + u32 textoff[7]; + u32 dataoff[11]; + + u8 *textmem[7]; + u8 *datamem[11]; + + u32 textsize[7]; + u32 datasize[11]; + + u8 *bssmem; + u32 bsssize; + + u32 entry; + + u32 unused[7]; +} dolheader; + +/* Entry pointer */ +typedef void (*dolentry)(void); + +/* Constants */ +#define DOL_INDEX 1 + + +void __Loader_SetLowMem(void) +{ + /* Setup low memory */ + *(vu32*)0x800000F8 = 0x0E7BE2C0; + *(vu32*)0x800000FC = 0x2B73A840; + + /* Flush cache */ + DCFlushRange((void *)(0x800000F8), 0xFF); + + // Set the clock + settime(secs_to_ticks(time(NULL) - 946684800)); + + // Remove 002 error + *(u16 *)0x80003140 = 0x0038; + *(u16 *)0x80003142 = 0xffff; + *(u16 *)0x80003188 = 0x0038; + *(u16 *)0x8000318A = 0xffff; + + DCFlushRange((void*)0x80003140, 4); + DCFlushRange((void*)0x80003188, 4); +} + +void __Loader_SetVMode(u64 tid) +{ + GXRModeObj *vmode_ptr = NULL; + u32 vmode_reg; + + u32 progressive, tvmode; + + /* Get video mode configuration */ + progressive = (CONF_GetProgressiveScan() > 0) && VIDEO_HaveComponentCable(); + tvmode = CONF_GetVideo(); + + /* Select video mode */ + switch(tid & 0xFF) { + /* PAL */ + case 'P': + vmode_reg = (CONF_GetEuRGB60() > 0) ? 5 : 1; + + if (tvmode != CONF_VIDEO_PAL) { + vmode_reg = 5; + vmode_ptr = &TVNtsc480IntDf; + } + + break; + + /* NTSC or unknown */ + case 'E': + case 'J': + vmode_reg = 0; + + if (tvmode != CONF_VIDEO_NTSC) + vmode_ptr = &TVEurgb60Hz480IntDf; + + break; + + default: + vmode_reg = 0; + } + + /* Override video mode */ + if (loaderCfg.forcePal50) { + vmode_reg = 1; + vmode_ptr = &TVPal528IntDf; + } + + if (loaderCfg.forcePal60) { + vmode_reg = 5; + vmode_ptr = &TVEurgb60Hz480IntDf; + } + + if (loaderCfg.forceNtsc) { + vmode_reg = 0; + vmode_ptr = &TVNtsc480IntDf; + } + + if (loaderCfg.forceHdtv) { + vmode_reg = 0; + vmode_ptr = &TVNtsc480Prog; + } + + + /* Set video mode register */ + *(vu32 *)0x800000CC = vmode_reg; + + /* Set video mode */ + if (vmode_ptr) { + /* If progressive available, use it */ + if (progressive) + vmode_ptr = &TVNtsc480Prog; + + Video_Configure(vmode_ptr); + Video_Clear(COLOR_BLACK); + } + + // Anti-green screen fix + VIDEO_SetBlack(TRUE); + VIDEO_Flush(); + VIDEO_WaitVSync(); +} + + +void __Loader_PatchDol(u8 *buffer, u32 len) +{ + struct config *cfg = &loaderCfg; + + /* Video mode patching */ + if (cfg->patchVmode) + Patch_VideoMode(buffer, len); +} + +s32 __Loader_ReadDol(dolentry *p_entry) +{ + static dolheader dol ATTRIBUTE_ALIGN(32); + + u32 cnt; + s32 cfd = -1, ret; + + /* Open DOL file */ + cfd = ES_OpenContent(DOL_INDEX); + if (cfd < 0) + return cfd; + + /* Read DOL header */ + ret = ES_ReadContent(cfd, (u8 *)&dol, sizeof(dol)); + if (ret < 0) + goto out; + + /* Clear BSS */ + memset(dol.bssmem, 0, dol.bsssize); + + /* Read TEXT section */ + for (cnt = 0; cnt < 7; cnt++) { + u32 offset = dol.textoff[cnt]; + + if (offset >= sizeof(dol)) { + u8 *buffer = (u8 *)dol.textmem[cnt]; + u32 len = dol.textsize[cnt]; + + /* Seek */ + ES_SeekContent(cfd, offset, 0); + + /* Read */ + ret = ES_ReadContent(cfd, buffer, len); + if (ret < 0) + goto out; + + /* Patch */ + __Loader_PatchDol(buffer, len); + } + } + + /* Read DATA section */ + for (cnt = 0; cnt < 11; cnt++) { + u32 offset = dol.dataoff[cnt]; + + if (offset >= sizeof(dol)) { + u8 *buffer = (u8 *)dol.datamem[cnt]; + u32 len = dol.datasize[cnt]; + + /* Seek */ + ES_SeekContent(cfd, offset, 0); + + /* Read */ + ret = ES_ReadContent(cfd, buffer, len); + if (ret < 0) + goto out; + + /* Patch */ + __Loader_PatchDol(buffer, len); + } + } + + /* Set entry point */ + *p_entry = (dolentry)dol.entry; + +out: + /* Close DOL file */ + if (cfd >= 0) + ES_CloseContent(cfd); + + return ret; +} + +void __Loader_Shutdown(void) +{ + /* Deinitialize ISFS */ + ISFS_Deinitialize(); + + /* Shutdown IOS subsystems */ + SYS_ResetSystem(SYS_SHUTDOWN, 0, 0); +} + + +s32 Loader_Execute(void) +{ + dolentry p_entry = NULL; + + u64 tid; + s32 ret; + + /* Get title ID */ + ret = ES_GetTitleID(&tid); + if (ret < 0) + return ret; + + /* Load DOL */ + ret = __Loader_ReadDol(&p_entry); + if (ret < 0) + return ret; + + /* Set low memory */ + __Loader_SetLowMem(); + + /* Set video mode */ + __Loader_SetVMode(tid); + + /* Shutdown subsystems */ + __Loader_Shutdown(); + + /* Clear screen */ + Video_Clear(COLOR_BLACK); + + /* Jump to the entry point */ + p_entry(); + + return 0; +} diff --git a/nand-loader/source/loader.h b/nand-loader/source/loader.h new file mode 100644 index 0000000..dd5457c --- /dev/null +++ b/nand-loader/source/loader.h @@ -0,0 +1,8 @@ +#ifndef _LOADER_H_ +#define _LOADER_H_ + +/* Prototypes */ +s32 Loader_Execute(void); + +#endif + diff --git a/nand-loader/source/nandloader.c b/nand-loader/source/nandloader.c new file mode 100644 index 0000000..af67cdf --- /dev/null +++ b/nand-loader/source/nandloader.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "config.h" +#include "gui.h" +#include "loader.h" +#include "sys.h" +#include "video.h" +#include "wpad.h" + +/* Constans */ +#define RESTART_SECONDS 4 + + +/* Override IOS_LoadStartupIOS */ +s32 __IOS_LoadStartupIOS() { return 0; } + + +void Menu(void) +{ + /* Initialize Wiimote */ + Wpad_Init(); + + /* Draw background */ + Gui_DrawBackground(); + + /* Initialize console */ + Gui_InitConsole(); + + /* Configuration menu */ + Config_Menu(); + + /* Destroy console */ + Con_Destroy(); + + /* Disconnect Wiimote */ + Wpad_Disconnect(); +} + +int main(int argc, char **argv) +{ + s32 ret; + + /* Initialize subsystems */ + Sys_Init(); + + /* Set video mode */ + Video_SetMode(); + + /* Initialize ISFS */ + ISFS_Initialize(); + + /* Read config */ + Config_Read(); + + /* Config menu */ + ret = SYS_ResetButtonDown(); + if (ret) + Menu(); + + /* Draw loading image */ + if(loaderCfg.showSplash) + Gui_DrawLoading(); + + /* Execute application */ + Loader_Execute(); + + /* Draw error image */ + Gui_DrawError(); + + /* Sleep */ + sleep(RESTART_SECONDS); + + /* Load System Menu */ + Sys_LoadMenu(); + + return 0; +} diff --git a/nand-loader/source/patches.c b/nand-loader/source/patches.c new file mode 100644 index 0000000..cd286b2 --- /dev/null +++ b/nand-loader/source/patches.c @@ -0,0 +1,126 @@ +#include +#include +#include + +#include "config.h" + +GXRModeObj *PAL2NTSC[] = { + &TVMpal480IntDf, &TVNtsc480IntDf, + &TVPal264Ds, &TVNtsc240Ds, + &TVPal264DsAa, &TVNtsc240DsAa, + &TVPal264Int, &TVNtsc240Int, + &TVPal264IntAa, &TVNtsc240IntAa, + &TVPal524IntAa, &TVNtsc480IntAa, + &TVPal528Int, &TVNtsc480IntAa, + &TVPal528IntDf, &TVNtsc480IntDf, + &TVPal574IntDfScale, &TVNtsc480IntDf, + &TVEurgb60Hz240Ds, &TVNtsc240Ds, + &TVEurgb60Hz240DsAa, &TVNtsc240DsAa, + &TVEurgb60Hz240Int, &TVNtsc240Int, + &TVEurgb60Hz240IntAa, &TVNtsc240IntAa, + &TVEurgb60Hz480Int, &TVNtsc480IntAa, + &TVEurgb60Hz480IntDf, &TVNtsc480IntDf, + &TVEurgb60Hz480IntAa, &TVNtsc480IntAa, + &TVEurgb60Hz480Prog, &TVNtsc480Prog, + &TVEurgb60Hz480ProgSoft,&TVNtsc480Prog, + &TVEurgb60Hz480ProgAa, &TVNtsc480Prog, + NULL, NULL +}; + +GXRModeObj *NTSC2PAL[] = { + &TVNtsc240Ds, &TVPal264Ds, + &TVNtsc240DsAa, &TVPal264DsAa, + &TVNtsc240Int, &TVPal264Int, + &TVNtsc240IntAa, &TVPal264IntAa, + &TVNtsc480IntDf, &TVPal528IntDf, + &TVNtsc480IntAa, &TVPal524IntAa, + &TVNtsc480Prog, &TVPal528IntDf, + NULL, NULL +}; + +GXRModeObj *NTSC2PAL60[]={ + &TVNtsc240Ds, &TVEurgb60Hz240Ds, + &TVNtsc240DsAa, &TVEurgb60Hz240DsAa, + &TVNtsc240Int, &TVEurgb60Hz240Int, + &TVNtsc240IntAa, &TVEurgb60Hz240IntAa, + &TVNtsc480IntDf, &TVEurgb60Hz480IntDf, + &TVNtsc480IntAa, &TVEurgb60Hz480IntAa, + &TVNtsc480Prog, &TVEurgb60Hz480Prog, + NULL, NULL +}; + + +void Patch_VideoMode(u8 *buffer, u32 len) +{ + GXRModeObj **table = NULL; + + u32 cnt, idx; + + /* Select conversion table */ + if (loaderCfg.forcePal50) + table = NTSC2PAL; + if (loaderCfg.forcePal60) + table = NTSC2PAL60; + if (loaderCfg.forceNtsc) + table = PAL2NTSC; + + /* No table selected */ + if (!table) + return; + + for (cnt = 0; cnt < len; cnt += 4) { + u8 *ptr = buffer + cnt; + + /* Search for video modes */ + for (idx = 0; table[idx]; idx += 2) { + /* Patch video mode */ + if (!memcmp(ptr, table[idx], sizeof(GXRModeObj))) + memcpy(ptr, table[idx+1], sizeof(GXRModeObj)); + } + } +} + +// void __Patch_VideoMode(u8 *buffer, u32 len) +// { +// const u8 pattern[] = { 0x54, 0x00, 0xFF, 0xFE }; +// +// u64 tid; +// u32 cnt; +// +// /* Get title ID */ +// ES_GetTitleID(&tid); +// +// for (cnt = 0; cnt < len; cnt += 4) { +// u32 *ptr = (u32 *)(buffer + cnt); +// +// /* Pattern found */ +// if (!memcmp(ptr, pattern, sizeof(pattern))) { +// u8 region = tid & 0xFF; +// +// /* USA region */ +// if (region == 'E') +// *ptr = 0x38000001; +// /* JAP region */ +// if (region == 'J') +// *ptr = 0x38000000; +// +// break; +// } +// } +// } +// +// +// void Patch_VideoMode(u8 *buffer, u32 len) +// { +// const u8 pattern[] = { 0x41, 0x82, 0x00, 0x0C, 0x41, 0x80, 0x00, 0x1C, 0x48, 0x00, 0x00, 0x18 }; +// +// u32 cnt; +// +// for (cnt = 0; cnt < len; cnt += 4) { +// u8 *ptr = buffer + cnt; +// +// /* Pattern found */ +// if (!memcmp(ptr, pattern, sizeof(pattern))) +// __Patch_VideoMode(ptr, len - cnt); +// } +// } diff --git a/nand-loader/source/patches.h b/nand-loader/source/patches.h new file mode 100644 index 0000000..78a271f --- /dev/null +++ b/nand-loader/source/patches.h @@ -0,0 +1,7 @@ +#ifndef _PATCHES_H_ +#define _PATCHES_H_ + +/* Prototypes */ +void Patch_VideoMode(u8 *, u32); + +#endif diff --git a/nand-loader/source/pngu.c b/nand-loader/source/pngu.c new file mode 100644 index 0000000..1bab952 --- /dev/null +++ b/nand-loader/source/pngu.c @@ -0,0 +1,1561 @@ +/******************************************************************************************** + +PNGU Version : 0.2a + +Coder : frontier + +More info : http://frontier-dev.net + +********************************************************************************************/ +#include +#include +#include +#include "png.h" +#include "pngu.h" +#include "pngu_impl.h" + +#ifndef SAFE_FREE +#define SAFE_FREE(p) if(p){free(p);p=NULL;} +#endif + +#if 0 +// moved to pngu_impl.h +// Constants +#define PNGU_SOURCE_BUFFER 1 +#define PNGU_SOURCE_DEVICE 2 + +// Prototypes of helper functions +int pngu_info (IMGCTX ctx); +int pngu_decode (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha); +int pngu_decode_add_alpha (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha, int force32bit); +void pngu_free_info (IMGCTX ctx); +void pngu_read_data_from_buffer (png_structp png_ptr, png_bytep data, png_size_t length); +void pngu_write_data_to_buffer (png_structp png_ptr, png_bytep data, png_size_t length); +void pngu_flush_data_to_buffer (png_structp png_ptr); +int pngu_clamp (int value, int min, int max); + + +// PNGU Image context struct +struct _IMGCTX +{ + int source; + void *buffer; + char *filename; + PNGU_u32 cursor; + PNGU_u32 buf_size; // buffer size + + PNGU_u32 propRead; + PNGUPROP prop; + + PNGU_u32 infoRead; + png_structp png_ptr; + png_infop info_ptr; + FILE *fd; + + png_bytep *row_pointers; + png_bytep img_data; +}; +#endif + + +// PNGU Implementation // + +IMGCTX PNGU_SelectImageFromBufferX (const void *buffer, int size) +{ + IMGCTX ctx = NULL; + + if (!buffer) + return NULL; + + ctx = calloc (sizeof (struct _IMGCTX), 1); + if (!ctx) + return NULL; + + ctx->buffer = (void *) buffer; + ctx->source = PNGU_SOURCE_BUFFER; + ctx->cursor = 0; + ctx->filename = NULL; + ctx->propRead = 0; + ctx->infoRead = 0; + ctx->buf_size = size; + + return ctx; +} + +IMGCTX PNGU_SelectImageFromBuffer (const void *buffer) +{ + return PNGU_SelectImageFromBufferX(buffer, 0); +} + + +IMGCTX PNGU_SelectImageFromDevice (const char *filename) +{ + IMGCTX ctx = NULL; + + if (!filename) + return NULL; + + ctx = calloc (sizeof (struct _IMGCTX), 1); + if (!ctx) + return NULL; + + ctx->buffer = NULL; + ctx->source = PNGU_SOURCE_DEVICE; + ctx->cursor = 0; + + ctx->filename = malloc (strlen (filename) + 1); + if (!ctx->filename) + { + free (ctx); + return NULL; + } + strcpy(ctx->filename, filename); + + ctx->propRead = 0; + ctx->infoRead = 0; + + return ctx; +} + + +void PNGU_ReleaseImageContext (IMGCTX ctx) +{ + if (!ctx) + return; + + if (ctx->filename) + free (ctx->filename); + + if ((ctx->propRead) && (ctx->prop.trans)) + free (ctx->prop.trans); + + pngu_free_info (ctx); + + free (ctx); +} + + +int PNGU_GetImageProperties (IMGCTX ctx, PNGUPROP *imgprop) +{ + int res; + + if (!ctx->propRead) + { + res = pngu_info (ctx); + if (res != PNGU_OK) + return res; + } + + *imgprop = ctx->prop; + + return PNGU_OK; +} + + +int PNGU_DecodeToYCbYCr (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride) +{ + int result; + PNGU_u32 x, y, buffWidth; + + // width needs to be divisible by two + if (width % 2) + return PNGU_ODD_WIDTH; + + // stride needs to be divisible by two + if (stride % 2) + return PNGU_ODD_STRIDE; + + result = pngu_decode (ctx, width, height, 1); + if (result != PNGU_OK) + return result; + + // Copy image to the output buffer + buffWidth = (width + stride) / 2; + for (y = 0; y < height; y++) + for (x = 0; x < (width / 2); x++) + ((PNGU_u32 *)buffer)[y*buffWidth+x] = PNGU_RGB8_TO_YCbYCr (*(ctx->row_pointers[y]+x*6), *(ctx->row_pointers[y]+x*6+1), *(ctx->row_pointers[y]+x*6+2), + *(ctx->row_pointers[y]+x*6+3), *(ctx->row_pointers[y]+x*6+4), *(ctx->row_pointers[y]+x*6+5)); + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +int PNGU_DecodeToRGB565 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride) +{ + int result; + PNGU_u32 x, y, buffWidth; + + result = pngu_decode (ctx, width, height, 1); + if (result != PNGU_OK) + return result; + + buffWidth = width + stride; + + // Copy image to the output buffer + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + ((PNGU_u16 *)buffer)[y*buffWidth+x] = + (((PNGU_u16) (ctx->row_pointers[y][x*3] & 0xF8)) << 8) | + (((PNGU_u16) (ctx->row_pointers[y][x*3+1] & 0xFC)) << 3) | + (((PNGU_u16) (ctx->row_pointers[y][x*3+2] & 0xF8)) >> 3); + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +int PNGU_DecodeToRGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride, PNGU_u8 default_alpha) +{ + int result; + PNGU_u32 x, y, buffWidth; + + result = pngu_decode (ctx, width, height, 0); + if (result != PNGU_OK) + return result; + + buffWidth = width + stride; + + // Check is source image has an alpha channel + if ( (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA) ) + { + // Alpha channel present, copy image to the output buffer + for (y = 0; y < height; y++) + memcpy (buffer + (y * buffWidth * 4), ctx->row_pointers[y], width * 4); + } + else + { + // No alpha channel present, copy image to the output buffer + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + ((PNGU_u32 *)buffer)[y*buffWidth+x] = + (((PNGU_u32) ctx->row_pointers[y][x*3]) << 24) | + (((PNGU_u32) ctx->row_pointers[y][x*3+1]) << 16) | + (((PNGU_u32) ctx->row_pointers[y][x*3+2]) << 8) | + ((PNGU_u32) default_alpha); + } + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +int PNGU_DecodeTo4x4RGB565 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer) +{ + int result; + PNGU_u32 x, y, qwidth, qheight; + + // width and height need to be divisible by four + if ((width % 4) || (height % 4)) + return PNGU_INVALID_WIDTH_OR_HEIGHT; + + result = pngu_decode (ctx, width, height, 1); + if (result != PNGU_OK) + return result; + + // Copy image to the output buffer + qwidth = width / 4; + qheight = height / 4; + + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 4; + + PNGU_u64 field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*12)); + PNGU_u64 field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase] = + (((field64 & 0xF800000000000000ULL) | ((field64 & 0xFC000000000000ULL) << 3) | ((field64 & 0xF80000000000ULL) << 5)) | + (((field64 & 0xF800000000ULL) << 8) | ((field64 & 0xFC000000ULL) << 11) | ((field64 & 0xF80000ULL) << 13)) | + (((field64 & 0xF800ULL) << 16) | ((field64 & 0xFCULL) << 19) | ((field32 & 0xF8000000ULL) >> 11)) | + (((field32 & 0xF80000ULL) >> 8) | ((field32 & 0xFC00ULL) >> 5) | ((field32 & 0xF8ULL) >> 3))); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+1]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+1] = + (((field64 & 0xF800000000000000ULL) | ((field64 & 0xFC000000000000ULL) << 3) | ((field64 & 0xF80000000000ULL) << 5)) | + (((field64 & 0xF800000000ULL) << 8) | ((field64 & 0xFC000000ULL) << 11) | ((field64 & 0xF80000ULL) << 13)) | + (((field64 & 0xF800ULL) << 16) | ((field64 & 0xFCULL) << 19) | ((field32 & 0xF8000000ULL) >> 11)) | + (((field32 & 0xF80000ULL) >> 8) | ((field32 & 0xFC00ULL) >> 5) | ((field32 & 0xF8ULL) >> 3))); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+2]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+2] = + (((field64 & 0xF800000000000000ULL) | ((field64 & 0xFC000000000000ULL) << 3) | ((field64 & 0xF80000000000ULL) << 5)) | + (((field64 & 0xF800000000ULL) << 8) | ((field64 & 0xFC000000ULL) << 11) | ((field64 & 0xF80000ULL) << 13)) | + (((field64 & 0xF800ULL) << 16) | ((field64 & 0xFCULL) << 19) | ((field32 & 0xF8000000ULL) >> 11)) | + (((field32 & 0xF80000ULL) >> 8) | ((field32 & 0xFC00ULL) >> 5) | ((field32 & 0xF8ULL) >> 3))); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+3]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+3] = + (((field64 & 0xF800000000000000ULL) | ((field64 & 0xFC000000000000ULL) << 3) | ((field64 & 0xF80000000000ULL) << 5)) | + (((field64 & 0xF800000000ULL) << 8) | ((field64 & 0xFC000000ULL) << 11) | ((field64 & 0xF80000ULL) << 13)) | + (((field64 & 0xF800ULL) << 16) | ((field64 & 0xFCULL) << 19) | ((field32 & 0xF8000000ULL) >> 11)) | + (((field32 & 0xF80000ULL) >> 8) | ((field32 & 0xFC00ULL) >> 5) | ((field32 & 0xF8ULL) >> 3))); + } + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +int PNGU_DecodeTo4x4RGB5A3 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha) +{ + int result; + PNGU_u32 x, y, qwidth, qheight; + PNGU_u64 alphaMask; + + // width and height need to be divisible by four + if ((width % 4) || (height % 4)) + return PNGU_INVALID_WIDTH_OR_HEIGHT; + + result = pngu_decode (ctx, width, height, 0); + if (result != PNGU_OK) + return result; + + // Init some vars + qwidth = width / 4; + qheight = height / 4; + + // Check is source image has an alpha channel + if ( (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA) ) + { + // Alpha channel present, copy image to the output buffer + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 4; + PNGU_u64 tmp; + + PNGU_u64 fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*16)); + PNGU_u64 fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*16+8)); + // If first pixel is opaque set MSB to 1 and encode colors in RGB555, else set MSB to 0 and encode colors in ARGB3444 + if ((fieldA & 0xE000000000ULL) == 0xE000000000ULL) + tmp = 0x8000000000000000ULL | ((fieldA & 0xF800000000000000ULL) >> 1) | ((fieldA & 0xF8000000000000ULL) << 2) | ((fieldA & 0xF80000000000ULL) << 5); + else + tmp = ((fieldA & 0xE000000000ULL) << 23) | ((fieldA & 0xF000000000000000ULL) >> 4) | (fieldA & 0xF0000000000000ULL) | ((fieldA & 0xF00000000000ULL) << 4); + + // If second pixel is opaque set MSB to 1 and encode colors in RGB555, else set MSB to 0 and encode colors in ARGB3444 + if ((fieldA & 0xE0ULL) == 0xE0ULL) + tmp = tmp | 0x800000000000ULL | ((fieldA & 0xF8000000ULL) << 15) | ((fieldA & 0xF80000ULL) << 18) | ((fieldA & 0xF800ULL) << 21); + else + tmp = tmp | ((fieldA & 0xE0ULL) << 39) | ((fieldA & 0xF0000000ULL) << 12) | ((fieldA & 0xF00000ULL) << 16) | ((fieldA & 0xF000ULL) << 20); + + // If third pixel is opaque set MSB to 1 and encode colors in RGB555, else set MSB to 0 and encode colors in ARGB3444 + if ((fieldB & 0xE000000000ULL) == 0xE000000000ULL) + tmp = tmp | 0x80000000ULL | ((fieldB & 0xF800000000000000ULL) >> 33) | ((fieldB & 0xF8000000000000ULL) >> 30) | ((fieldB & 0xF80000000000ULL) >> 27); + else + tmp = tmp | ((fieldB & 0xE000000000ULL) >> 9) | ((fieldB & 0xF000000000000000ULL) >> 36) | ((fieldB & 0xF0000000000000ULL) >> 32) | ((fieldB & 0xF00000000000ULL) >> 28); + + // If fourth pixel is opaque set MSB to 1 and encode colors in RGB555, else set MSB to 0 and encode colors in ARGB3444 + if ((fieldB & 0xE0ULL) == 0xE0ULL) + tmp = tmp | 0x8000ULL | ((fieldB & 0xF8000000ULL) >> 17) | ((fieldB & 0xF80000ULL) >> 14) | ((fieldB & 0xF800ULL) >> 11); + else + tmp = tmp | ((fieldB & 0xE0ULL) << 7) | ((fieldB & 0xF0000000ULL) >> 20) | ((fieldB & 0xF00000ULL) >> 16) | ((fieldB & 0xF000ULL) >> 12); + ((PNGU_u64 *) buffer)[blockbase] = tmp; + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*16+8)); + if ((fieldA & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = 0x8000000000000000ULL | ((fieldA & 0xF800000000000000ULL) >> 1) | ((fieldA & 0xF8000000000000ULL) << 2) | ((fieldA & 0xF80000000000ULL) << 5); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = ((fieldA & 0xE000000000ULL) << 23) | ((fieldA & 0xF000000000000000ULL) >> 4) | (fieldA & 0xF0000000000000ULL) | ((fieldA & 0xF00000000000ULL) << 4); + + if ((fieldA & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x800000000000ULL | ((fieldA & 0xF8000000ULL) << 15) | ((fieldA & 0xF80000ULL) << 18) | ((fieldA & 0xF800ULL) << 21); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldA & 0xE0ULL) << 39) | ((fieldA & 0xF0000000ULL) << 12) | ((fieldA & 0xF00000ULL) << 16) | ((fieldA & 0xF000ULL) << 20); + + if ((fieldB & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x80000000ULL | ((fieldB & 0xF800000000000000ULL) >> 33) | ((fieldB & 0xF8000000000000ULL) >> 30) | ((fieldB & 0xF80000000000ULL) >> 27); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE000000000ULL) >> 9) | ((fieldB & 0xF000000000000000ULL) >> 36) | ((fieldB & 0xF0000000000000ULL) >> 32) | ((fieldB & 0xF00000000000ULL) >> 28); + + if ((fieldB & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x8000ULL | ((fieldB & 0xF8000000ULL) >> 17) | ((fieldB & 0xF80000ULL) >> 14) | ((fieldB & 0xF800ULL) >> 11); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE0ULL) << 7) | ((fieldB & 0xF0000000ULL) >> 20) | ((fieldB & 0xF00000ULL) >> 16) | ((fieldB & 0xF000ULL) >> 12); + ((PNGU_u64 *) buffer)[blockbase+1] = tmp; + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*16+8)); + if ((fieldA & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = 0x8000000000000000ULL | ((fieldA & 0xF800000000000000ULL) >> 1) | ((fieldA & 0xF8000000000000ULL) << 2) | ((fieldA & 0xF80000000000ULL) << 5); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = ((fieldA & 0xE000000000ULL) << 23) | ((fieldA & 0xF000000000000000ULL) >> 4) | (fieldA & 0xF0000000000000ULL) | ((fieldA & 0xF00000000000ULL) << 4); + + if ((fieldA & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x800000000000ULL | ((fieldA & 0xF8000000ULL) << 15) | ((fieldA & 0xF80000ULL) << 18) | ((fieldA & 0xF800ULL) << 21); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldA & 0xE0ULL) << 39) | ((fieldA & 0xF0000000ULL) << 12) | ((fieldA & 0xF00000ULL) << 16) | ((fieldA & 0xF000ULL) << 20); + + if ((fieldB & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x80000000ULL | ((fieldB & 0xF800000000000000ULL) >> 33) | ((fieldB & 0xF8000000000000ULL) >> 30) | ((fieldB & 0xF80000000000ULL) >> 27); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE000000000ULL) >> 9) | ((fieldB & 0xF000000000000000ULL) >> 36) | ((fieldB & 0xF0000000000000ULL) >> 32) | ((fieldB & 0xF00000000000ULL) >> 28); + + if ((fieldB & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x8000ULL | ((fieldB & 0xF8000000ULL) >> 17) | ((fieldB & 0xF80000ULL) >> 14) | ((fieldB & 0xF800ULL) >> 11); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE0ULL) << 7) | ((fieldB & 0xF0000000ULL) >> 20) | ((fieldB & 0xF00000ULL) >> 16) | ((fieldB & 0xF000ULL) >> 12); + ((PNGU_u64 *) buffer)[blockbase+2] = tmp; + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*16+8)); + if ((fieldA & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = 0x8000000000000000ULL | ((fieldA & 0xF800000000000000ULL) >> 1) | ((fieldA & 0xF8000000000000ULL) << 2) | ((fieldA & 0xF80000000000ULL) << 5); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = ((fieldA & 0xE000000000ULL) << 23) | ((fieldA & 0xF000000000000000ULL) >> 4) | (fieldA & 0xF0000000000000ULL) | ((fieldA & 0xF00000000000ULL) << 4); + + if ((fieldA & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x800000000000ULL | ((fieldA & 0xF8000000ULL) << 15) | ((fieldA & 0xF80000ULL) << 18) | ((fieldA & 0xF800ULL) << 21); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldA & 0xE0ULL) << 39) | ((fieldA & 0xF0000000ULL) << 12) | ((fieldA & 0xF00000ULL) << 16) | ((fieldA & 0xF000ULL) << 20); + + if ((fieldB & 0xE000000000ULL) == 0xE000000000ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x80000000ULL | ((fieldB & 0xF800000000000000ULL) >> 33) | ((fieldB & 0xF8000000000000ULL) >> 30) | ((fieldB & 0xF80000000000ULL) >> 27); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE000000000ULL) >> 9) | ((fieldB & 0xF000000000000000ULL) >> 36) | ((fieldB & 0xF0000000000000ULL) >> 32) | ((fieldB & 0xF00000000000ULL) >> 28); + + if ((fieldB & 0xE0ULL) == 0xE0ULL) + // Opaque pixel, so set MSB to 1 and encode colors in RGB555 + tmp = tmp | 0x8000ULL | ((fieldB & 0xF8000000ULL) >> 17) | ((fieldB & 0xF80000ULL) >> 14) | ((fieldB & 0xF800ULL) >> 11); + else + // Tranlucid pixel, so set MSB to 0 and encode colors in ARGB3444 + tmp = tmp | ((fieldB & 0xE0ULL) << 7) | ((fieldB & 0xF0000000ULL) >> 20) | ((fieldB & 0xF00000ULL) >> 16) | ((fieldB & 0xF000ULL) >> 12); + ((PNGU_u64 *) buffer)[blockbase+3] = tmp; + } + } + else + { + // No alpha channel present, copy image to the output buffer + default_alpha = (default_alpha >> 5); + if (default_alpha == 7) + { + // The user wants an opaque texture, so set MSB to 1 and encode colors in RGB555 + alphaMask = 0x8000800080008000ULL; + + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 4; + + PNGU_u64 field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*12)); + PNGU_u64 field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase] = + alphaMask | ((field64 & 0xF800000000000000ULL) >> 1) | ((field64 & 0xF8000000000000ULL) << 2) | + ((field64 & 0xF80000000000ULL) << 5) | ((field64 & 0xF800000000ULL) << 7) | ((field64 & 0xF8000000ULL) << 10) | + ((field64 & 0xF80000ULL) << 13) | ((field64 & 0xF800ULL) << 15) | ((field64 & 0xF8ULL) << 18) | + ((field32 & 0xF8000000ULL) >> 11) | ((field32 & 0xF80000ULL) >> 9) | ((field32 & 0xF800ULL) >> 6) | ((field32 & 0xF8ULL) >> 3); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+1]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+1] = + alphaMask | ((field64 & 0xF800000000000000ULL) >> 1) | ((field64 & 0xF8000000000000ULL) << 2) | + ((field64 & 0xF80000000000ULL) << 5) | ((field64 & 0xF800000000ULL) << 7) | ((field64 & 0xF8000000ULL) << 10) | + ((field64 & 0xF80000ULL) << 13) | ((field64 & 0xF800ULL) << 15) | ((field64 & 0xF8ULL) << 18) | + ((field32 & 0xF8000000ULL) >> 11) | ((field32 & 0xF80000ULL) >> 9) | ((field32 & 0xF800ULL) >> 6) | ((field32 & 0xF8ULL) >> 3); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+2]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+2] = + alphaMask | ((field64 & 0xF800000000000000ULL) >> 1) | ((field64 & 0xF8000000000000ULL) << 2) | + ((field64 & 0xF80000000000ULL) << 5) | ((field64 & 0xF800000000ULL) << 7) | ((field64 & 0xF8000000ULL) << 10) | + ((field64 & 0xF80000ULL) << 13) | ((field64 & 0xF800ULL) << 15) | ((field64 & 0xF8ULL) << 18) | + ((field32 & 0xF8000000ULL) >> 11) | ((field32 & 0xF80000ULL) >> 9) | ((field32 & 0xF800ULL) >> 6) | ((field32 & 0xF8ULL) >> 3); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+3]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+3] = + alphaMask | ((field64 & 0xF800000000000000ULL) >> 1) | ((field64 & 0xF8000000000000ULL) << 2) | + ((field64 & 0xF80000000000ULL) << 5) | ((field64 & 0xF800000000ULL) << 7) | ((field64 & 0xF8000000ULL) << 10) | + ((field64 & 0xF80000ULL) << 13) | ((field64 & 0xF800ULL) << 15) | ((field64 & 0xF8ULL) << 18) | + ((field32 & 0xF8000000ULL) >> 11) | ((field32 & 0xF80000ULL) >> 9) | ((field32 & 0xF800ULL) >> 6) | ((field32 & 0xF8ULL) >> 3); + } + } + else + { + // The user wants a translucid texture, so set MSB to 0 and encode colors in ARGB3444 + default_alpha = (default_alpha << 4); + alphaMask = (((PNGU_u64) default_alpha) << 56) | (((PNGU_u64) default_alpha) << 40) | + (((PNGU_u64) default_alpha) << 24) | (((PNGU_u64) default_alpha) << 8); + + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 4; + + PNGU_u64 field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*12)); + PNGU_u64 field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase] = + alphaMask | ((field64 & 0xF000000000000000ULL) >> 4) | (field64 & 0xF0000000000000ULL) | ((field64 & 0xF00000000000ULL) << 4) | + ((field64 & 0xF000000000ULL) << 4) | ((field64 & 0xF0000000ULL) << 8) | ((field64 & 0xF00000ULL) << 12) | + ((field64 & 0xF000ULL) << 12) | ((field64 & 0xF0ULL) << 16) | ((field32 & 0xF0000000ULL) >> 12) | + ((field32 & 0xF00000ULL) >> 12) | ((field32 & 0xF000ULL) >> 8) | ((field32 & 0xF0ULL) >> 4); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+1]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+1] = + alphaMask | ((field64 & 0xF000000000000000ULL) >> 4) | (field64 & 0xF0000000000000ULL) | ((field64 & 0xF00000000000ULL) << 4) | + ((field64 & 0xF000000000ULL) << 4) | ((field64 & 0xF0000000ULL) << 8) | ((field64 & 0xF00000ULL) << 12) | + ((field64 & 0xF000ULL) << 12) | ((field64 & 0xF0ULL) << 16) | ((field32 & 0xF0000000ULL) >> 12) | + ((field32 & 0xF00000ULL) >> 12) | ((field32 & 0xF000ULL) >> 8) | ((field32 & 0xF0ULL) >> 4); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+2]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+2] = + alphaMask | ((field64 & 0xF000000000000000ULL) >> 4) | (field64 & 0xF0000000000000ULL) | ((field64 & 0xF00000000000ULL) << 4) | + ((field64 & 0xF000000000ULL) << 4) | ((field64 & 0xF0000000ULL) << 8) | ((field64 & 0xF00000ULL) << 12) | + ((field64 & 0xF000ULL) << 12) | ((field64 & 0xF0ULL) << 16) | ((field32 & 0xF0000000ULL) >> 12) | + ((field32 & 0xF00000ULL) >> 12) | ((field32 & 0xF000ULL) >> 8) | ((field32 & 0xF0ULL) >> 4); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+3]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+3] = + alphaMask | ((field64 & 0xF000000000000000ULL) >> 4) | (field64 & 0xF0000000000000ULL) | ((field64 & 0xF00000000000ULL) << 4) | + ((field64 & 0xF000000000ULL) << 4) | ((field64 & 0xF0000000ULL) << 8) | ((field64 & 0xF00000ULL) << 12) | + ((field64 & 0xF000ULL) << 12) | ((field64 & 0xF0ULL) << 16) | ((field32 & 0xF0000000ULL) >> 12) | + ((field32 & 0xF00000ULL) >> 12) | ((field32 & 0xF000ULL) >> 8) | ((field32 & 0xF0ULL) >> 4); + } + } + } + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +int PNGU_DecodeTo4x4RGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha) +{ + int result; + PNGU_u32 x, y, qwidth, qheight; + PNGU_u64 alphaMask; + + // width and height need to be divisible by four + if ((width % 4) || (height % 4)) + return PNGU_INVALID_WIDTH_OR_HEIGHT; + + result = pngu_decode (ctx, width, height, 0); + if (result != PNGU_OK) + return result; + + // Init some variables + qwidth = width / 4; + qheight = height / 4; + + // Check is source image has an alpha channel + if ( (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA) ) + { + // Alpha channel present, copy image to the output buffer + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 8; + + PNGU_u64 fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*16)); + PNGU_u64 fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*16+8)); + ((PNGU_u64 *) buffer)[blockbase] = + ((fieldA & 0xFF00000000ULL) << 24) | ((fieldA & 0xFF00000000000000ULL) >> 8) | + ((fieldA & 0xFFULL) << 40) | ((fieldA & 0xFF000000ULL) << 8) | + ((fieldB & 0xFF00000000ULL) >> 8) | ((fieldB & 0xFF00000000000000ULL) >> 40) | + ((fieldB & 0xFFULL) << 8) | ((fieldB & 0xFF000000ULL) >> 24); + ((PNGU_u64 *) buffer)[blockbase+4] = + ((fieldA & 0xFFFF0000000000ULL) << 8) | ((fieldA & 0xFFFF00ULL) << 24) | + ((fieldB & 0xFFFF0000000000ULL) >> 24) | ((fieldB & 0xFFFF00ULL) >> 8); + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*16+8)); + ((PNGU_u64 *) buffer)[blockbase+1] = + ((fieldA & 0xFF00000000ULL) << 24) | ((fieldA & 0xFF00000000000000ULL) >> 8) | + ((fieldA & 0xFFULL) << 40) | ((fieldA & 0xFF000000ULL) << 8) | + ((fieldB & 0xFF00000000ULL) >> 8) | ((fieldB & 0xFF00000000000000ULL) >> 40) | + ((fieldB & 0xFFULL) << 8) | ((fieldB & 0xFF000000ULL) >> 24); + ((PNGU_u64 *) buffer)[blockbase+5] = + ((fieldA & 0xFFFF0000000000ULL) << 8) | ((fieldA & 0xFFFF00ULL) << 24) | + ((fieldB & 0xFFFF0000000000ULL) >> 24) | ((fieldB & 0xFFFF00ULL) >> 8); + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*16+8)); + ((PNGU_u64 *) buffer)[blockbase+2] = + ((fieldA & 0xFF00000000ULL) << 24) | ((fieldA & 0xFF00000000000000ULL) >> 8) | + ((fieldA & 0xFFULL) << 40) | ((fieldA & 0xFF000000ULL) << 8) | + ((fieldB & 0xFF00000000ULL) >> 8) | ((fieldB & 0xFF00000000000000ULL) >> 40) | + ((fieldB & 0xFFULL) << 8) | ((fieldB & 0xFF000000ULL) >> 24); + ((PNGU_u64 *) buffer)[blockbase+6] = + ((fieldA & 0xFFFF0000000000ULL) << 8) | ((fieldA & 0xFFFF00ULL) << 24) | + ((fieldB & 0xFFFF0000000000ULL) >> 24) | ((fieldB & 0xFFFF00ULL) >> 8); + + fieldA = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*16)); + fieldB = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*16+8)); + ((PNGU_u64 *) buffer)[blockbase+3] = + ((fieldA & 0xFF00000000ULL) << 24) | ((fieldA & 0xFF00000000000000ULL) >> 8) | + ((fieldA & 0xFFULL) << 40) | ((fieldA & 0xFF000000ULL) << 8) | + ((fieldB & 0xFF00000000ULL) >> 8) | ((fieldB & 0xFF00000000000000ULL) >> 40) | + ((fieldB & 0xFFULL) << 8) | ((fieldB & 0xFF000000ULL) >> 24); + ((PNGU_u64 *) buffer)[blockbase+7] = + ((fieldA & 0xFFFF0000000000ULL) << 8) | ((fieldA & 0xFFFF00ULL) << 24) | + ((fieldB & 0xFFFF0000000000ULL) >> 24) | ((fieldB & 0xFFFF00ULL) >> 8); + } + } + else + { + // No alpha channel present, copy image to the output buffer + alphaMask = (((PNGU_u64)default_alpha) << 56) | (((PNGU_u64)default_alpha) << 40) | + (((PNGU_u64)default_alpha) << 24) | (((PNGU_u64)default_alpha) << 8); + + for (y = 0; y < qheight; y++) + for (x = 0; x < qwidth; x++) + { + int blockbase = (y * qwidth + x) * 8; + + PNGU_u64 field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4]+x*12)); + PNGU_u64 field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase] = + (((field64 & 0xFF00000000000000ULL) >> 8) | (field64 & 0xFF00000000ULL) | + ((field64 & 0xFF00ULL) << 8) | ((field32 & 0xFF0000ULL) >> 16) | alphaMask); + ((PNGU_u64 *) buffer)[blockbase+4] = + (((field64 & 0xFFFF0000000000ULL) << 8) | ((field64 & 0xFFFF0000ULL) << 16) | + ((field64 & 0xFFULL) << 24) | ((field32 & 0xFF000000ULL) >> 8) | (field32 & 0xFFFFULL)); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+1]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+1]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+1] = + (((field64 & 0xFF00000000000000ULL) >> 8) | (field64 & 0xFF00000000ULL) | + ((field64 & 0xFF00ULL) << 8) | ((field32 & 0xFF0000ULL) >> 16) | alphaMask); + ((PNGU_u64 *) buffer)[blockbase+5] = + (((field64 & 0xFFFF0000000000ULL) << 8) | ((field64 & 0xFFFF0000ULL) << 16) | + ((field64 & 0xFFULL) << 24) | ((field32 & 0xFF000000ULL) >> 8) | (field32 & 0xFFFFULL)); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+2]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+2]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+2] = + (((field64 & 0xFF00000000000000ULL) >> 8) | (field64 & 0xFF00000000ULL) | + ((field64 & 0xFF00ULL) << 8) | ((field32 & 0xFF0000ULL) >> 16) | alphaMask); + ((PNGU_u64 *) buffer)[blockbase+6] = + (((field64 & 0xFFFF0000000000ULL) << 8) | ((field64 & 0xFFFF0000ULL) << 16) | + ((field64 & 0xFFULL) << 24) | ((field32 & 0xFF000000ULL) >> 8) | (field32 & 0xFFFFULL)); + + field64 = *((PNGU_u64 *)(ctx->row_pointers[y*4+3]+x*12)); + field32 = (PNGU_u64) *((PNGU_u32 *)(ctx->row_pointers[y*4+3]+x*12+8)); + ((PNGU_u64 *) buffer)[blockbase+3] = + (((field64 & 0xFF00000000000000ULL) >> 8) | (field64 & 0xFF00000000ULL) | + ((field64 & 0xFF00ULL) << 8) | ((field32 & 0xFF0000ULL) >> 16) | alphaMask); + ((PNGU_u64 *) buffer)[blockbase+7] = + (((field64 & 0xFFFF0000000000ULL) << 8) | ((field64 & 0xFFFF0000ULL) << 16) | + ((field64 & 0xFFULL) << 24) | ((field32 & 0xFF000000ULL) >> 8) | (field32 & 0xFFFFULL)); + } + } + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +//######################################################################################## +//---------- Start CMPR added section --------------------------------------------------- +//######################################################################################## + +static inline PNGU_u16 rgb8ToRGB565(PNGU_u8 *color) +{ + return ((color[0] >> 3) << 11) | ((color[1] >> 2) << 5) | (color[2] >> 3); +} + +static int colorDistance(const PNGU_u8 *c0, const PNGU_u8 *c1) +{ + return (c1[0] - c0[0]) * (c1[0] - c0[0]) + (c1[1] - c0[1]) * (c1[1] - c0[1]) + (c1[2] - c0[2]) * (c1[2] - c0[2]); +} + +static void getBaseColors(PNGU_u8 *color0, PNGU_u8 *color1, const PNGU_u8 *srcBlock) +{ + int maxDistance = -1; + int i; + int j; + + for (i = 0; i < 15; ++i) + for (j = i + 1; j < 16; ++j) + { + int distance = colorDistance(srcBlock + i * 4, srcBlock + j * 4); + if (distance > maxDistance) + { + maxDistance = distance; + *(PNGU_u32 *)color0 = ((PNGU_u32 *)srcBlock)[i]; + *(PNGU_u32 *)color1 = ((PNGU_u32 *)srcBlock)[j]; + } + } + if (rgb8ToRGB565(color0) < rgb8ToRGB565(color1)) + { + PNGU_u32 tmp; + tmp = *(PNGU_u32 *)color0; + *(PNGU_u32 *)color0 = *(PNGU_u32 *)color1; + *(PNGU_u32 *)color1 = tmp; + } +} + +static PNGU_u32 colorIndices(const PNGU_u8 *color0, const PNGU_u8 *color1, const PNGU_u8 *srcBlock) +{ + PNGU_u16 colors[4][4]; + PNGU_u32 res = 0; + int i; + + // Make the 4 colors available in the block + colors[0][0] = (color0[0] & 0xF8) | (color0[0] >> 5); + colors[0][1] = (color0[1] & 0xFC) | (color0[1] >> 6); + colors[0][2] = (color0[2] & 0xF8) | (color0[2] >> 5); + colors[1][0] = (color1[0] & 0xF8) | (color1[0] >> 5); + colors[1][1] = (color1[1] & 0xFC) | (color1[1] >> 6); + colors[1][2] = (color1[2] & 0xF8) | (color1[2] >> 5); + colors[2][0] = (2 * colors[0][0] + 1 * colors[1][0]) / 3; + colors[2][1] = (2 * colors[0][1] + 1 * colors[1][1]) / 3; + colors[2][2] = (2 * colors[0][2] + 1 * colors[1][2]) / 3; + colors[3][0] = (1 * colors[0][0] + 2 * colors[1][0]) / 3; + colors[3][1] = (1 * colors[0][1] + 2 * colors[1][1]) / 3; + colors[3][2] = (1 * colors[0][2] + 2 * colors[1][2]) / 3; + for (i = 15; i >= 0; --i) + { + int c0 = srcBlock[i * 4 + 0]; + int c1 = srcBlock[i * 4 + 1]; + int c2 = srcBlock[i * 4 + 2]; + int d0 = abs(colors[0][0] - c0) + abs(colors[0][1] - c1) + abs(colors[0][2] - c2); + int d1 = abs(colors[1][0] - c0) + abs(colors[1][1] - c1) + abs(colors[1][2] - c2); + int d2 = abs(colors[2][0] - c0) + abs(colors[2][1] - c1) + abs(colors[2][2] - c2); + int d3 = abs(colors[3][0] - c0) + abs(colors[3][1] - c1) + abs(colors[3][2] - c2); + int b0 = d0 > d3; + int b1 = d1 > d2; + int b2 = d0 > d2; + int b3 = d1 > d3; + int b4 = d2 > d3; + int x0 = b1 & b2; + int x1 = b0 & b3; + int x2 = b0 & b4; + res |= (x2 | ((x0 | x1) << 1)) << ((15 - i) << 1); + } + return res; +} + +int PNGU_DecodeToCMPR_Trim(IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer) +{ + int result; + PNGU_u8 srcBlock[16 * 4]; + PNGU_u8 color0[4]; + PNGU_u8 color1[4]; + PNGU_u8 *outBuf = (PNGU_u8 *)buffer; + int ii; + int jj; + int k; + + //check for alpha channel + result = pngu_decode_add_alpha (ctx, width, height, 0, 1); + if (result != PNGU_OK) + return result; + + // trim down + width = width & ~7u; + height = height & ~7u; + + // Alpha channel present, copy image to the output buffer + for (jj = 0; jj < height; jj += 8) + for (ii = 0; ii < width; ii += 8) + for (k = 0; k < 4; ++k) + { + int j = jj + ((k >> 1) << 2); // jj + 0, jj + 0, jj + 4, jj + 4 + int i = ii + ((k & 1) << 2); // ii + 0, ii + 4, ii + 0, ii + 4 + memcpy(srcBlock, ctx->row_pointers[j] + i * 4, 16); + memcpy(srcBlock + 4 * 4, ctx->row_pointers[j + 1] + i * 4, 16); + memcpy(srcBlock + 8 * 4, ctx->row_pointers[j + 2] + i * 4, 16); + memcpy(srcBlock + 12 * 4, ctx->row_pointers[j + 3] + i * 4, 16); + getBaseColors(color0, color1, srcBlock); + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color0); + outBuf += 2; + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color1); + outBuf += 2; + *(PNGU_u32 *)outBuf = colorIndices(color0, color1, srcBlock); + outBuf += 4; + } + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + + +// if width or height is not divisible by 8 +// then the remaining will be padded with last row/column +// buffer must be allocated with width and height rounded up + +int PNGU_DecodeToCMPR_Pad(IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer) +{ + int result; + PNGU_u8 srcBlock[16 * 4]; + PNGU_u8 color0[4]; + PNGU_u8 color1[4]; + PNGU_u8 *outBuf = (PNGU_u8 *)buffer; + int ii; + int jj; + int k; + + //check for alpha channel + result = pngu_decode_add_alpha (ctx, width, height, 0, 1); + if (result != PNGU_OK) + return result; + + // Alpha channel present, copy image to the output buffer + for (jj = 0; jj < height; jj += 8) { + for (ii = 0; ii < width; ii += 8) { + for (k = 0; k < 4; ++k) { + // k(i,j) + // 0(0,0) 1(4,0) + // 2(4,0) 3(4,4) + int i = ii + ((k & 1) << 2); // ii + 0, ii + 4, ii + 0, ii + 4 + int j = jj + ((k >> 1) << 2); // jj + 0, jj + 0, jj + 4, jj + 4 + int ny; // 4 lines + int px = 4; // num columns to copy + if (i >= width) i = width - 1; + if (i + px > width) px = width - i; + for (ny=0; ny<4; ny++) { + if (j >= height) j = height - 1; + memcpy(srcBlock + ny * 4 * 4, + ctx->row_pointers[j] + i * 4, px * 4); + if (px < 4) { + // repeat last column (4-px) times + int x = width - 1; + int nx; + for (nx = px; nx < 4; nx++) { + memcpy(srcBlock + ny * 4 * 4 + nx * 4, + ctx->row_pointers[j] + x * 4, 4); + } + } + j++; + } + getBaseColors(color0, color1, srcBlock); + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color0); + outBuf += 2; + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color1); + outBuf += 2; + *(PNGU_u32 *)outBuf = colorIndices(color0, color1, srcBlock); + outBuf += 4; + } + } + } + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + + // Success + return PNGU_OK; +} + +void ExtractBlock( PNGU_u8 *inPtr, int y, int x, PNGU_u32 width, int i, PNGU_u8 colorBlock[] ) { + PNGU_u32 offset; + PNGU_u8 r, g, b, a; + + offset = (((y >> 2)<<4)*width) + ((x >> 2)<<6) + ((((y&3) << 2) + (x&3) ) << 1); + //offset = (((y >> 2) << 4)*width) + ((x >> 2) << 6) + (((y % 4 << 2) + x % 4) << 1); + //get rgba values based on the RGBA8 offsets + a = *(inPtr+offset); + r = *(inPtr+offset+1); + g = *(inPtr+offset+32); + b = *(inPtr+offset+33); + colorBlock[i*4] = r; + colorBlock[i*4+1] = g; + colorBlock[i*4+2] = b; + colorBlock[i*4+3] = a; + +} + +/** + * by usptactical + * Converts a 4x4 RGBA8 image to CMPR. + */ +int PNGU_4x4RGBA8_To_CMPR(void *buf_rgb, PNGU_u32 width, PNGU_u32 height, void *buf_cmpr) +{ + PNGU_u8 srcBlock[16 * 4]; + PNGU_u8 color0[4]; + PNGU_u8 color1[4]; + PNGU_u8 *outBuf = (PNGU_u8 *)buf_cmpr; + PNGU_u8 *rgba = (PNGU_u8 *)buf_rgb; + int jj, ii, i, j, k; + + width = width & ~7u; + height = height & ~7u; + + // loop over blocks + //CMPR needs 4x4 block of pixels: + //image row 0: 0, 1, 2, 3 (first 16 block) + //image row 1: 0, 1, 2, 3 (second 16 block) + //image row 2: 0, 1, 2, 3 (third 16 block) + //image row 3: 0, 1, 2, 3 (last 16 block) + + //image row 0: 4, 5, 6, 7 (first 16 block) + //image row 1: 4, 5, 6, 7 (second 16 block) + //image row 2: 4, 5, 6, 7 (third 16 block) + //image row 3: 4, 5, 6, 7 (last 16 block) + + //image row 4: 0, 1, 2, 3 (first 16 block) + //image row 5: 0, 1, 2, 3 (second 16 block) + //image row 6: 0, 1, 2, 3 (third 16 block) + //image row 7: 0, 1, 2, 3 (last 16 block) + + //image row 4: 4, 5, 6, 7 (first 16 block) + //image row 5: 4, 5, 6, 7 (second 16 block) + //image row 6: 4, 5, 6, 7 (third 16 block) + //image row 7: 4, 5, 6, 7 (last 16 block) + + for(jj = 0; jj < height; jj += 8) + for(ii = 0; ii < width; ii += 8) + for (k=0; k < 4; k++) + { + j = jj + ((k >> 1) << 2); // jj + 0, jj + 0, jj + 4, jj + 4 + i = ii + ((k & 1) << 2); // ii + 0, ii + 4, ii + 0, ii + 4 + + ExtractBlock(rgba, j, i, width, 0, srcBlock); + ExtractBlock(rgba, j, i+1, width, 1, srcBlock); + ExtractBlock(rgba, j, i+2, width, 2, srcBlock); + ExtractBlock(rgba, j, i+3, width, 3, srcBlock); + + ExtractBlock(rgba, j+1, i, width, 4, srcBlock); + ExtractBlock(rgba, j+1, i+1, width, 5, srcBlock); + ExtractBlock(rgba, j+1, i+2, width, 6, srcBlock); + ExtractBlock(rgba, j+1, i+3, width, 7, srcBlock); + + ExtractBlock(rgba, j+2, i, width, 8, srcBlock); + ExtractBlock(rgba, j+2, i+1, width, 9, srcBlock); + ExtractBlock(rgba, j+2, i+2, width, 10, srcBlock); + ExtractBlock(rgba, j+2, i+3, width, 11, srcBlock); + + ExtractBlock(rgba, j+3, i, width, 12, srcBlock); + ExtractBlock(rgba, j+3, i+1, width, 13, srcBlock); + ExtractBlock(rgba, j+3, i+2, width, 14, srcBlock); + ExtractBlock(rgba, j+3, i+3, width, 15, srcBlock); + + getBaseColors(color0, color1, srcBlock); + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color0); + outBuf += 2; + *(PNGU_u16 *)outBuf = rgb8ToRGB565(color1); + outBuf += 2; + *(PNGU_u32 *)outBuf = colorIndices(color0, color1, srcBlock); + outBuf += 4; + } + // Success + return PNGU_OK; +} + +// if width or height is not divisible by 8 +// then the remaining will be padded with last row/column +// buffer must be allocated with width and height rounded up +int PNGU_RGBA8_To_CMPR(void *buf_rgb, PNGU_u32 width, PNGU_u32 height, void *buf_cmpr) +{ + PNGU_u8 srcBlock[16 * 4]; + PNGU_u8 color0[4]; + PNGU_u8 color1[4]; + PNGU_u8 *src, *block; + PNGU_u8 *cmpr = (PNGU_u8 *)buf_cmpr; + PNGU_u8 *rgba = (PNGU_u8 *)buf_rgb; + int jj, ii, i, j, k; + int x, y; // counter + int px, py; // pixel coord + + for(jj = 0; jj < height; jj += 8) { + for(ii = 0; ii < width; ii += 8) { + for (k=0; k < 4; k++) { + i = ii + ((k & 1) << 2); // ii + 0, ii + 4, ii + 0, ii + 4 + j = jj + ((k >> 1) << 2); // jj + 0, jj + 0, jj + 4, jj + 4 + + block = srcBlock; + for (y=0; y<4; y++) { + py = j + y; + if (py >= height) py = height - 1; + src = rgba + py * width * 4; + for (x=0; x<4; x++) { + px = i + x; + if (px >= width) px = width - 1; + memcpy(block, src + px * 4, 4); + block += 4; + } + } + + getBaseColors(color0, color1, srcBlock); + *(PNGU_u16 *)cmpr = rgb8ToRGB565(color0); + cmpr += 2; + *(PNGU_u16 *)cmpr = rgb8ToRGB565(color1); + cmpr += 2; + *(PNGU_u32 *)cmpr = colorIndices(color0, color1, srcBlock); + cmpr += 4; + } + } + } + // Success + return PNGU_OK; +} + + +/** + * added by usptactical + * handles png error messages + */ +void user_error (png_structp png_ptr, png_const_charp c) +{ + longjmp(png_jmpbuf(png_ptr), 1); +} + + + +//######################################################################################## +//---------- End CMPR added section ----------------------------------------------------- +//######################################################################################## + + +int PNGU_EncodeFromYCbYCr (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride) +{ + png_uint_32 rowbytes; + PNGU_u32 x, y, buffWidth; + + // Erase from the context any readed info + pngu_free_info (ctx); + ctx->propRead = 0; + + // Check if the user has selected a file to write the image + if (ctx->source == PNGU_SOURCE_BUFFER); + + else if (ctx->source == PNGU_SOURCE_DEVICE) + { + // Open file + if (!(ctx->fd = fopen (ctx->filename, "wb"))) + return PNGU_CANT_OPEN_FILE; + } + + else + return PNGU_NO_FILE_SELECTED; + + // Allocation of libpng structs + ctx->png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!(ctx->png_ptr)) + { + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_LIB_ERROR; + } + + ctx->info_ptr = png_create_info_struct (ctx->png_ptr); + if (!(ctx->info_ptr)) + { + png_destroy_write_struct (&(ctx->png_ptr), (png_infopp)NULL); + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_LIB_ERROR; + } + + if (ctx->source == PNGU_SOURCE_BUFFER) + { + // Installation of our custom data writer function + ctx->cursor = 0; + png_set_write_fn (ctx->png_ptr, ctx, pngu_write_data_to_buffer, pngu_flush_data_to_buffer); + } + else if (ctx->source == PNGU_SOURCE_DEVICE) + { + // Default data writer uses function fwrite, so it needs to use our FILE* + png_init_io (ctx->png_ptr, ctx->fd); + } + + // Setup output file properties + png_set_IHDR (ctx->png_ptr, ctx->info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + // Allocate memory to store the image in RGB format + rowbytes = width * 3; + if (rowbytes % 4) + rowbytes = ((rowbytes / 4) + 1) * 4; // Add extra padding so each row starts in a 4 byte boundary + + ctx->img_data = malloc (rowbytes * height); + if (!ctx->img_data) + { + png_destroy_write_struct (&(ctx->png_ptr), (png_infopp)NULL); + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_LIB_ERROR; + } + + ctx->row_pointers = malloc (sizeof (png_bytep) * height); + if (!ctx->row_pointers) + { + png_destroy_write_struct (&(ctx->png_ptr), (png_infopp)NULL); + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_LIB_ERROR; + } + + // Encode YCbYCr image into RGB8 format + buffWidth = (width + stride) / 2; + for (y = 0; y < height; y++) + { + ctx->row_pointers[y] = ctx->img_data + (y * rowbytes); + + for (x = 0; x < (width / 2); x++) + PNGU_YCbYCr_TO_RGB8 ( ((PNGU_u32 *)buffer)[y*buffWidth+x], + ((PNGU_u8 *) ctx->row_pointers[y]+x*6), ((PNGU_u8 *) ctx->row_pointers[y]+x*6+1), + ((PNGU_u8 *) ctx->row_pointers[y]+x*6+2), ((PNGU_u8 *) ctx->row_pointers[y]+x*6+3), + ((PNGU_u8 *) ctx->row_pointers[y]+x*6+4), ((PNGU_u8 *) ctx->row_pointers[y]+x*6+5) ); + } + + // Tell libpng where is our image data + png_set_rows (ctx->png_ptr, ctx->info_ptr, ctx->row_pointers); + + // Write file header and image data + png_write_png (ctx->png_ptr, ctx->info_ptr, PNG_TRANSFORM_IDENTITY, NULL); + + // Tell libpng we have no more data to write + png_write_end (ctx->png_ptr, (png_infop) NULL); + + // Free resources + free (ctx->img_data); + free (ctx->row_pointers); + png_destroy_write_struct (&(ctx->png_ptr), &(ctx->info_ptr)); + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + + // Success + return PNGU_OK; +} + + +// This function is taken from a libogc example +PNGU_u32 PNGU_RGB8_TO_YCbYCr (PNGU_u8 r1, PNGU_u8 g1, PNGU_u8 b1, PNGU_u8 r2, PNGU_u8 g2, PNGU_u8 b2) +{ + int y1, cb1, cr1, y2, cb2, cr2, cb, cr; + + y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000; + cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000; + cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000; + + y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000; + cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000; + cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000; + + cb = (cb1 + cb2) >> 1; + cr = (cr1 + cr2) >> 1; + + return (PNGU_u32) ((y1 << 24) | (cb << 16) | (y2 << 8) | cr); +} + + +void PNGU_YCbYCr_TO_RGB8 (PNGU_u32 ycbycr, PNGU_u8 *r1, PNGU_u8 *g1, PNGU_u8 *b1, PNGU_u8 *r2, PNGU_u8 *g2, PNGU_u8 *b2) +{ + PNGU_u8 *val = (PNGU_u8 *) &ycbycr; + int r, g, b; + + r = 1.371f * (val[3] - 128); + g = - 0.698f * (val[3] - 128) - 0.336f * (val[1] - 128); + b = 1.732f * (val[1] - 128); + + *r1 = pngu_clamp (val[0] + r, 0, 255); + *g1 = pngu_clamp (val[0] + g, 0, 255); + *b1 = pngu_clamp (val[0] + b, 0, 255); + + *r2 = pngu_clamp (val[2] + r, 0, 255); + *g2 = pngu_clamp (val[2] + g, 0, 255); + *b2 = pngu_clamp (val[2] + b, 0, 255); +} + + +int pngu_info (IMGCTX ctx) +{ + png_byte magic[8]; + png_uint_32 width; + png_uint_32 height; + png_color_16p background; + png_bytep trans; + png_color_16p trans_values; + int scale, i; + + // Check if there is a file selected and if it is a valid .png + if (ctx->source == PNGU_SOURCE_BUFFER) + memcpy (magic, ctx->buffer, 8); + + else if (ctx->source == PNGU_SOURCE_DEVICE) + { + // Open file + if (!(ctx->fd = fopen (ctx->filename, "rb"))) + return PNGU_CANT_OPEN_FILE; + + // Load first 8 bytes into magic buffer + if (fread (magic, 1, 8, ctx->fd) != 8) + { + fclose (ctx->fd); + return PNGU_CANT_READ_FILE; + } + } + + else + return PNGU_NO_FILE_SELECTED;; + + if (png_sig_cmp(magic, 0, 8) != 0) + { + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_FILE_IS_NOT_PNG; + } + + // Allocation of libpng structs + ctx->png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!(ctx->png_ptr)) + { + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + return PNGU_LIB_ERROR; + } + + ctx->info_ptr = png_create_info_struct (ctx->png_ptr); + if (!(ctx->info_ptr)) + { + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + png_destroy_read_struct (&(ctx->png_ptr), (png_infopp)NULL, (png_infopp)NULL); + return PNGU_LIB_ERROR; + } + + if (ctx->source == PNGU_SOURCE_BUFFER) + { + // Installation of our custom data provider function + ctx->cursor = 0; + png_set_read_fn (ctx->png_ptr, ctx, pngu_read_data_from_buffer); + } + else if (ctx->source == PNGU_SOURCE_DEVICE) + { + // Default data provider uses function fread, so it needs to use our FILE* + png_init_io (ctx->png_ptr, ctx->fd); + png_set_sig_bytes (ctx->png_ptr, 8); // We have read 8 bytes already to check PNG authenticity + } + + // Read png header + png_read_info (ctx->png_ptr, ctx->info_ptr); + + // Query image properties if they have not been queried before + if (!ctx->propRead) + { + png_get_IHDR(ctx->png_ptr, ctx->info_ptr, &width, &height, + (int *) &(ctx->prop.imgBitDepth), + (int *) &(ctx->prop.imgColorType), + NULL, NULL, NULL); + + ctx->prop.imgWidth = width; + ctx->prop.imgHeight = height; + switch (ctx->prop.imgColorType) + { + case PNG_COLOR_TYPE_GRAY: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_GRAY; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_GRAY_ALPHA; + break; + case PNG_COLOR_TYPE_PALETTE: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_PALETTE; + break; + case PNG_COLOR_TYPE_RGB: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_RGB; + break; + case PNG_COLOR_TYPE_RGB_ALPHA: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_RGB_ALPHA; + break; + default: + ctx->prop.imgColorType = PNGU_COLOR_TYPE_UNKNOWN; + break; + } + + // Constant used to scale 16 bit values to 8 bit values + scale = 1; + if (ctx->prop.imgBitDepth == 16) + scale = 256; + + // Query background color, if any. + ctx->prop.validBckgrnd = 0; + if (((ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA)) && + (png_get_bKGD (ctx->png_ptr, ctx->info_ptr, &background))) + { + ctx->prop.validBckgrnd = 1; + ctx->prop.bckgrnd.r = background->red / scale; + ctx->prop.bckgrnd.g = background->green / scale; + ctx->prop.bckgrnd.b = background->blue / scale; + } + else if (((ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA)) && + (png_get_bKGD (ctx->png_ptr, ctx->info_ptr, &background))) + { + ctx->prop.validBckgrnd = 1; + ctx->prop.bckgrnd.r = ctx->prop.bckgrnd.g = ctx->prop.bckgrnd.b = background->gray / scale; + } + + // Query list of transparent colors, if any. + ctx->prop.numTrans = 0; + ctx->prop.trans = NULL; + if (((ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA)) && + (png_get_tRNS (ctx->png_ptr, ctx->info_ptr, &trans, (int *) &(ctx->prop.numTrans), &trans_values))) + { + if (ctx->prop.numTrans) + { + ctx->prop.trans = malloc (sizeof (PNGUCOLOR) * ctx->prop.numTrans); + if (ctx->prop.trans) + for (i = 0; i < ctx->prop.numTrans; i++) + { + ctx->prop.trans[i].r = trans_values[i].red / scale; + ctx->prop.trans[i].g = trans_values[i].green / scale; + ctx->prop.trans[i].b = trans_values[i].blue / scale; + } + else + ctx->prop.numTrans = 0; + } + } + else if (((ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA)) && + (png_get_tRNS (ctx->png_ptr, ctx->info_ptr, &trans, (int *) &(ctx->prop.numTrans), &trans_values))) + { + if (ctx->prop.numTrans) + { + ctx->prop.trans = malloc (sizeof (PNGUCOLOR) * ctx->prop.numTrans); + if (ctx->prop.trans) + for (i = 0; i < ctx->prop.numTrans; i++) + ctx->prop.trans[i].r = ctx->prop.trans[i].g = ctx->prop.trans[i].b = + trans_values[i].gray / scale; + else + ctx->prop.numTrans = 0; + } + } + + ctx->propRead = 1; + } + + // Success + ctx->infoRead = 1; + + return PNGU_OK; +} + + +int pngu_decode_add_alpha (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha, int force32bit) +{ + png_uint_32 rowbytes; + int i; + int chunk; + int rowsLeft; + png_bytep *curRow; + int mem_err = 0; + + + // Read info if it hasn't been read before + if (!ctx->infoRead) + { + i = pngu_info (ctx); + if (i != PNGU_OK) + return i; + } + + // Check if the user has specified the real width and height of the image + if ( (ctx->prop.imgWidth != width) || (ctx->prop.imgHeight != height) ) + return PNGU_INVALID_WIDTH_OR_HEIGHT; + + // Check if color type is supported by PNGU + if ( (ctx->prop.imgColorType == PNGU_COLOR_TYPE_PALETTE) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_UNKNOWN) ) + return PNGU_UNSUPPORTED_COLOR_TYPE; + + //************************************************* + //* added by usptactical to catch corrupted pngs * + jmp_buf save_jmp; + memcpy(save_jmp, png_jmpbuf(ctx->png_ptr), sizeof(save_jmp)); + if (setjmp(png_jmpbuf(ctx->png_ptr))) { + error: + memcpy(png_jmpbuf(ctx->png_ptr), save_jmp, sizeof(save_jmp)); + SAFE_FREE(ctx->row_pointers); + SAFE_FREE(ctx->img_data); + pngu_free_info (ctx); + //printf("*** This is a corrupted image!!\n"); sleep(5); + return (mem_err)?PNGU_LIB_ERROR:-666; + } + //override default error handler to suppress warning messages from libpng + png_set_error_fn (ctx->png_ptr, NULL, user_error, user_error); + //************************************************* + + // Scale 16 bit samples to 8 bit + if (ctx->prop.imgBitDepth == 16) + png_set_strip_16 (ctx->png_ptr); + + // Remove alpha channel if we don't need it + if (stripAlpha && ((ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB_ALPHA) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA))) + png_set_strip_alpha (ctx->png_ptr); + + // Expand 1, 2 and 4 bit samples to 8 bit + if (ctx->prop.imgBitDepth < 8) + png_set_packing (ctx->png_ptr); + + // Transform grayscale images to RGB + if ( (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY) || (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY_ALPHA) ) + png_set_gray_to_rgb (ctx->png_ptr); + + // Transform RBG images to RGBA + if (force32bit && (ctx->prop.imgColorType == PNGU_COLOR_TYPE_GRAY || ctx->prop.imgColorType == PNGU_COLOR_TYPE_RGB)) + png_set_filler(ctx->png_ptr, 0xFF, PNG_FILLER_AFTER); + + // Flush transformations + png_read_update_info (ctx->png_ptr, ctx->info_ptr); + + // Allocate memory to store the image + rowbytes = png_get_rowbytes (ctx->png_ptr, ctx->info_ptr); + if (rowbytes % 4) + rowbytes = ((rowbytes / 4) + 1) * 4; // Add extra padding so each row starts in a 4 byte boundary + + ctx->img_data = malloc (rowbytes * ctx->prop.imgHeight); + if (!ctx->img_data) + { + //pngu_free_info (ctx); + //return PNGU_LIB_ERROR; + mem_err = 1; + goto error; + } + + ctx->row_pointers = malloc (sizeof (png_bytep) * ctx->prop.imgHeight); + if (!ctx->row_pointers) + { + //free (ctx->img_data); + //pngu_free_info (ctx); + //return PNGU_LIB_ERROR; + mem_err = 1; + goto error; + } + + for (i = 0; i < (int)ctx->prop.imgHeight; i++) + ctx->row_pointers[i] = ctx->img_data + (i * rowbytes); + + // Transform the image and copy it to our allocated memory + if (png_get_interlace_type(ctx->png_ptr, ctx->info_ptr) != PNG_INTERLACE_NONE) + png_read_image (ctx->png_ptr, ctx->row_pointers); + else + { + rowsLeft = ctx->prop.imgHeight; + curRow = ctx->row_pointers; + while (rowsLeft > 0) + { + chunk = rowsLeft > 0x80 ? 0x80 : rowsLeft; + png_read_rows(ctx->png_ptr, curRow, NULL, chunk); + //usleep(1000); + curRow += chunk; + rowsLeft -= chunk; + } + } + + // restore default error handling + memcpy(png_jmpbuf(ctx->png_ptr), save_jmp, sizeof(save_jmp)); + + // Free resources + pngu_free_info (ctx); + + // Success + return PNGU_OK; +} + +int pngu_decode (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha) +{ + return pngu_decode_add_alpha(ctx, width, height, stripAlpha, 0); +} + + +void pngu_free_info (IMGCTX ctx) +{ + if (ctx->infoRead) + { + if (ctx->source == PNGU_SOURCE_DEVICE) + fclose (ctx->fd); + + png_destroy_read_struct (&(ctx->png_ptr), &(ctx->info_ptr), (png_infopp)NULL); + + ctx->infoRead = 0; + } +} + + +// Custom data provider function used for reading from memory buffers. +void pngu_read_data_from_buffer (png_structp png_ptr, png_bytep data, png_size_t length) +{ + IMGCTX ctx = (IMGCTX) png_get_io_ptr (png_ptr); + if (ctx->buf_size && (ctx->cursor + length > ctx->buf_size)) + { + static char err_str[40]; + snprintf(err_str, sizeof(err_str), "read error (%x/%x)", + ctx->cursor + length, ctx->buf_size); + png_error(png_ptr, err_str); + } + memcpy (data, ctx->buffer + ctx->cursor, length); + ctx->cursor += length; +} + + +// Custom data writer function used for writing to memory buffers. +void pngu_write_data_to_buffer (png_structp png_ptr, png_bytep data, png_size_t length) +{ + IMGCTX ctx = (IMGCTX) png_get_io_ptr (png_ptr); + memcpy (ctx->buffer + ctx->cursor, data, length); + ctx->cursor += length; +} + + +// Custom data flusher function used for writing to memory buffers. +void pngu_flush_data_to_buffer (png_structp png_ptr) +{ + // Nothing to do here +} + + +// Function used in YCbYCr to RGB decoding +int pngu_clamp (int value, int min, int max) +{ + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; +} + diff --git a/nand-loader/source/pngu.h b/nand-loader/source/pngu.h new file mode 100644 index 0000000..e979374 --- /dev/null +++ b/nand-loader/source/pngu.h @@ -0,0 +1,181 @@ +/******************************************************************************************** + +PNGU Version : 0.2a + +Coder : frontier + +More info : http://frontier-dev.net + +********************************************************************************************/ +#ifndef __PNGU__ +#define __PNGU__ + +// Return codes +#define PNGU_OK 0 +#define PNGU_ODD_WIDTH 1 +#define PNGU_ODD_STRIDE 2 +#define PNGU_INVALID_WIDTH_OR_HEIGHT 3 +#define PNGU_FILE_IS_NOT_PNG 4 +#define PNGU_UNSUPPORTED_COLOR_TYPE 5 +#define PNGU_NO_FILE_SELECTED 6 +#define PNGU_CANT_OPEN_FILE 7 +#define PNGU_CANT_READ_FILE 8 +#define PNGU_LIB_ERROR 9 + +// Color types +#define PNGU_COLOR_TYPE_GRAY 1 +#define PNGU_COLOR_TYPE_GRAY_ALPHA 2 +#define PNGU_COLOR_TYPE_PALETTE 3 +#define PNGU_COLOR_TYPE_RGB 4 +#define PNGU_COLOR_TYPE_RGB_ALPHA 5 +#define PNGU_COLOR_TYPE_UNKNOWN 6 + + +#ifdef __cplusplus + extern "C" { +#endif + +// Types +typedef unsigned char PNGU_u8; +typedef unsigned short PNGU_u16; +typedef unsigned int PNGU_u32; +typedef unsigned long long PNGU_u64; + +typedef struct +{ + PNGU_u8 r; + PNGU_u8 g; + PNGU_u8 b; +} PNGUCOLOR; + +typedef struct +{ + PNGU_u32 imgWidth; // In pixels + PNGU_u32 imgHeight; // In pixels + PNGU_u32 imgBitDepth; // In bitx + PNGU_u32 imgColorType; // PNGU_COLOR_TYPE_* + PNGU_u32 validBckgrnd; // Non zero if there is a background color + PNGUCOLOR bckgrnd; // Backgroun color + PNGU_u32 numTrans; // Number of transparent colors + PNGUCOLOR *trans; // Transparent colors +} PNGUPROP; + +// Image context, always initialize with SelectImageFrom* and free with ReleaseImageContext +struct _IMGCTX; +typedef struct _IMGCTX *IMGCTX; + + +/**************************************************************************** +* Pixel conversion * +****************************************************************************/ + +// Macro to convert RGB8 values to RGB565 +#define PNGU_RGB8_TO_RGB565(r,g,b) ( ((((PNGU_u16) r) & 0xF8U) << 8) | ((((PNGU_u16) g) & 0xFCU) << 3) | (((PNGU_u16) b) >> 3) ) + +// Macro to convert RGBA8 values to RGB5A3 +#define PNGU_RGB8_TO_RGB5A3(r,g,b,a) (PNGU_u16) (((a & 0xE0U) == 0xE0U) ? \ + (0x8000U | ((((PNGU_u16) r) & 0xF8U) << 7) | ((((PNGU_u16) g) & 0xF8U) << 2) | (((PNGU_u16) b) >> 3)) : \ + (((((PNGU_u16) a) & 0xE0U) << 7) | ((((PNGU_u16) r) & 0xF0U) << 4) | (((PNGU_u16) g) & 0xF0U) | ((((PNGU_u16) b) & 0xF0U) >> 4))) + +// Function to convert two RGB8 values to YCbYCr +PNGU_u32 PNGU_RGB8_TO_YCbYCr (PNGU_u8 r1, PNGU_u8 g1, PNGU_u8 b1, PNGU_u8 r2, PNGU_u8 g2, PNGU_u8 b2); + +// Function to convert an YCbYCr to two RGB8 values. +void PNGU_YCbYCr_TO_RGB8 (PNGU_u32 ycbycr, PNGU_u8 *r1, PNGU_u8 *g1, PNGU_u8 *b1, PNGU_u8 *r2, PNGU_u8 *g2, PNGU_u8 *b2); + + +/**************************************************************************** +* Image context handling * +****************************************************************************/ + +// Selects a PNG file, previosly loaded into a buffer, and creates an image context for subsequent procesing. +IMGCTX PNGU_SelectImageFromBuffer (const void *buffer); +IMGCTX PNGU_SelectImageFromBufferX (const void *buffer, int size); + +// Selects a PNG file, from any devoptab device, and creates an image context for subsequent procesing. +IMGCTX PNGU_SelectImageFromDevice (const char *filename); + +// Frees resources associated with an image context. Always call this function when you no longer need the IMGCTX. +void PNGU_ReleaseImageContext (IMGCTX ctx); + + +/**************************************************************************** +* Miscelaneous * +****************************************************************************/ + +// Retrieves info from selected PNG file, including image dimensions, color format, background and transparency colors. +int PNGU_GetImageProperties (IMGCTX ctx, PNGUPROP *fileproperties); + + +/**************************************************************************** +* Image conversion * +****************************************************************************/ + +// Expands selected image into an YCbYCr buffer. You need to specify context, image dimensions, +// destination address and stride in pixels (stride = buffer width - image width). +int PNGU_DecodeToYCbYCr (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride); + +// Macro for decoding an image inside a buffer at given coordinates. +#define PNGU_DECODE_TO_COORDS_YCbYCr(ctx,coordX,coordY,imgWidth,imgHeight,bufferWidth,bufferHeight,buffer) \ + \ + PNGU_DecodeToYCbYCr (ctx, imgWidth, imgHeight, ((void *) buffer) + (coordY) * (bufferWidth) * 2 + \ + (coordX) * 2, (bufferWidth) - (imgWidth)) + +// Expands selected image into a linear RGB565 buffer. You need to specify context, image dimensions, +// destination address and stride in pixels (stride = buffer width - image width). +int PNGU_DecodeToRGB565 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride); + +// Macro for decoding an image inside a buffer at given coordinates. +#define PNGU_DECODE_TO_COORDS_RGB565(ctx,coordX,coordY,imgWidth,imgHeight,bufferWidth,bufferHeight,buffer) \ + \ + PNGU_DecodeToRGB565 (ctx, imgWidth, imgHeight, ((void *) buffer) + (coordY) * (bufferWidth) * 2 + \ + (coordX) * 2, (bufferWidth) - (imgWidth)) + +// Expands selected image into a linear RGBA8 buffer. You need to specify context, image dimensions, +// destination address, stride in pixels and default alpha value, which is used if the source image +// doesn't have an alpha channel. +int PNGU_DecodeToRGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride, PNGU_u8 default_alpha); + +// Macro for decoding an image inside a buffer at given coordinates. +#define PNGU_DECODE_TO_COORDS_RGBA8(ctx,coordX,coordY,imgWidth,imgHeight,default_alpha,bufferWidth,bufferHeight,buffer) \ + \ + PNGU_DecodeToRGBA8 (ctx, imgWidth, imgHeight, ((void *) buffer) + (coordY) * (bufferWidth) * 4 + \ + (coordX) * 4, (bufferWidth) - (imgWidth), default_alpha) + +// Expands selected image into a 4x4 tiled RGB565 buffer. You need to specify context, image dimensions +// and destination address. +int PNGU_DecodeTo4x4RGB565 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer); +// Compressed version (DXT1/CMPR) +int PNGU_DecodeToCMPR_Trim(IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer); +int PNGU_DecodeToCMPR_Pad(IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer); +int PNGU_4x4RGBA8_To_CMPR(void *buf_rgb, PNGU_u32 width, PNGU_u32 height, void *buf_cmpr); +int PNGU_RGBA8_To_CMPR(void *buf_rgb, PNGU_u32 width, PNGU_u32 height, void *buf_cmpr); + +// Expands selected image into a 4x4 tiled RGB5A3 buffer. You need to specify context, image dimensions, +// destination address and default alpha value, which is used if the source image doesn't have an alpha channel. +int PNGU_DecodeTo4x4RGB5A3 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha); + +// Expands selected image into a 4x4 tiled RGBA8 buffer. You need to specify context, image dimensions, +// destination address and default alpha value, which is used if the source image doesn't have an alpha channel. +int PNGU_DecodeTo4x4RGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u8 default_alpha); + +// Encodes an YCbYCr image in PNG format and stores it in the selected device or memory buffer. You need to +// specify context, image dimensions, destination address and stride in pixels (stride = buffer width - image width). +int PNGU_EncodeFromYCbYCr (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride); + +// Macro for encoding an image stored into an YCbYCr buffer at given coordinates. +#define PNGU_ENCODE_TO_COORDS_YCbYCr(ctx,coordX,coordY,imgWidth,imgHeight,bufferWidth,bufferHeight,buffer) \ + \ + PNGU_EncodeFromYCbYCr (ctx, imgWidth, imgHeight, ((void *) buffer) + (coordY) * (bufferWidth) * 2 + \ + (coordX) * 2, (bufferWidth) - (imgWidth)) + +PNGU_u8 * PNGU_DecodeTo4x4RGBA8_EX (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, int * dstWidth, int * dstHeight, PNGU_u8 *dstPtr); + +int PNGU_EncodeFromEFB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stride); + +#ifdef __cplusplus + } +#endif + +#endif + diff --git a/nand-loader/source/pngu_impl.h b/nand-loader/source/pngu_impl.h new file mode 100644 index 0000000..782e94d --- /dev/null +++ b/nand-loader/source/pngu_impl.h @@ -0,0 +1,42 @@ +#ifndef __PNGU_I__ +#define __PNGU_I__ + +#include "png.h" + +// Constants +#define PNGU_SOURCE_BUFFER 1 +#define PNGU_SOURCE_DEVICE 2 + +// Prototypes of helper functions +int pngu_info (IMGCTX ctx); +int pngu_decode (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha); +int pngu_decode_add_alpha (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha, int force32bit); +void pngu_free_info (IMGCTX ctx); +void pngu_read_data_from_buffer (png_structp png_ptr, png_bytep data, png_size_t length); +void pngu_write_data_to_buffer (png_structp png_ptr, png_bytep data, png_size_t length); +void pngu_flush_data_to_buffer (png_structp png_ptr); +int pngu_clamp (int value, int min, int max); + + +// PNGU Image context struct +struct _IMGCTX +{ + int source; + void *buffer; + char *filename; + PNGU_u32 cursor; + PNGU_u32 buf_size; // buffer size + + PNGU_u32 propRead; + PNGUPROP prop; + + PNGU_u32 infoRead; + png_structp png_ptr; + png_infop info_ptr; + FILE *fd; + + png_bytep *row_pointers; + png_bytep img_data; +}; + +#endif diff --git a/nand-loader/source/stub.S b/nand-loader/source/stub.S new file mode 100644 index 0000000..756bf5a --- /dev/null +++ b/nand-loader/source/stub.S @@ -0,0 +1,118 @@ + .rodata + + .globl bgData +bgData: + .incbin "../data/background" + + .globl loadingImg +loadingImg: + .incbin "../data/loading" + + .globl errorImg +errorImg: + .incbin "../data/error" + + + +#include "asm.h" + + .text + .section .stub + + .extern _start + .globl _stub_start +_stub_start: + lis r3, 0x0011 + ori r3, r3, 0x0C64 + mtspr HID0, r3 + isync + + li r4, 0x2000 + mtmsr r4 + + ori r3, r3, 0xC000 + mtspr HID0, r3 + isync + + li r0, 0 + mtspr IBAT0U, r0 + mtspr IBAT1U, r0 + mtspr IBAT2U, r0 + mtspr IBAT3U, r0 + mtspr IBAT4U, r0 + mtspr IBAT5U, r0 + mtspr DBAT0U, r0 + mtspr DBAT1U, r0 + mtspr DBAT2U, r0 + mtspr DBAT3U, r0 + mtspr DBAT4U, r0 + mtspr DBAT5U, r0 + isync + + lis r0, 0x8000 + mtsr 0, r0 + mtsr 1, r0 + mtsr 2, r0 + mtsr 3, r0 + mtsr 4, r0 + mtsr 5, r0 + mtsr 6, r0 + mtsr 7, r0 + mtsr 8, r0 + mtsr 9, r0 + mtsr 10, r0 + mtsr 11, r0 + mtsr 12, r0 + mtsr 13, r0 + mtsr 14, r0 + mtsr 15, r0 + isync + + li r3, 2 + lis r4, 0x8000 + ori r4, r4, 0x1FFF + mtspr DBAT0L, r3 + mtspr DBAT0U, r4 + isync + mtspr IBAT0L, r3 + mtspr IBAT0U, r4 + isync + + addis r3, r3, 0x1000 + addis r4, r4, 0x1000 + mtspr DBAT4L, r3 + mtspr DBAT4U, r4 + isync + mtspr IBAT4L, r3 + mtspr IBAT4U, r4 + isync + + li r3, 0x2A + lis r4, 0xC000 + ori r4, r4, 0x1FFF + mtspr DBAT1L, r3 + mtspr DBAT1U, r4 + isync + mtspr IBAT1L, r3 + mtspr IBAT1U, r4 + isync + + addis r3, r3, 0x1000 + addis r4, r4, 0x1000 + mtspr DBAT2L, r3 + mtspr DBAT2U, r4 + isync + mtspr IBAT2L, r3 + mtspr IBAT2U, r4 + isync + + li r3, 0 + mfmsr r3 + ori r3, r3, MSR_DR|MSR_IR + mtsrr1 r3 + + lis r3, _start@h + ori r3, r3, _start@l + mtsrr0 r3 + + rfi diff --git a/nand-loader/source/sys.c b/nand-loader/source/sys.c new file mode 100644 index 0000000..8a9e54b --- /dev/null +++ b/nand-loader/source/sys.c @@ -0,0 +1,48 @@ +#include +#include +#include + +/* Constants */ +#define CERTS_LEN 0x280 + +/* Variables */ +static const char certs_fs[] ATTRIBUTE_ALIGN(32) = "/sys/cert.sys"; + + +void Sys_Init(void) +{ + /* Initialize video subsytem */ + VIDEO_Init(); +} + +void Sys_LoadMenu(void) +{ + /* Return to the Wii system menu */ + SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0); +} + +s32 Sys_GetCerts(signed_blob **certs, u32 *len) +{ + static signed_blob certificates[CERTS_LEN] ATTRIBUTE_ALIGN(32); + + s32 fd, ret; + + /* Open certificates file */ + fd = IOS_Open(certs_fs, 1); + if (fd < 0) + return fd; + + /* Read certificates */ + ret = IOS_Read(fd, certificates, sizeof(certificates)); + + /* Close file */ + IOS_Close(fd); + + /* Set values */ + if (ret > 0) { + *certs = certificates; + *len = sizeof(certificates); + } + + return ret; +} diff --git a/nand-loader/source/sys.h b/nand-loader/source/sys.h new file mode 100644 index 0000000..c1ac84c --- /dev/null +++ b/nand-loader/source/sys.h @@ -0,0 +1,9 @@ +#ifndef _SYS_H_ +#define _SYS_H_ + +/* Prototypes */ +void Sys_Init(void); +void Sys_LoadMenu(void); +s32 Sys_GetCerts(signed_blob **, u32 *); + +#endif diff --git a/nand-loader/source/title.c b/nand-loader/source/title.c new file mode 100644 index 0000000..0d05bd9 --- /dev/null +++ b/nand-loader/source/title.c @@ -0,0 +1,256 @@ +#include +#include +#include +#include +#include + +#include "utils.h" + + +s32 Title_GetList(u64 **outbuf, u32 *outlen) +{ + u64 *titles = NULL; + + u32 len, nb_titles; + s32 ret; + + /* Get number of titles */ + ret = ES_GetNumTitles(&nb_titles); + if (ret < 0) + return ret; + + /* Calculate buffer lenght */ + len = round_up(sizeof(u64) * nb_titles, 32); + + /* Allocate memory */ + titles = memalign(32, len); + if (!titles) + return -1; + + /* Get titles */ + ret = ES_GetTitles(titles, nb_titles); + if (ret < 0) + goto err; + + /* Set values */ + *outbuf = titles; + *outlen = nb_titles; + + return 0; + +err: + /* Free memory */ + if (titles) + free(titles); + + return ret; +} + +s32 Title_GetTicketViews(u64 tid, tikview **outbuf, u32 *outlen) +{ + tikview *views = NULL; + + u32 nb_views; + s32 ret; + + /* Get number of ticket views */ + ret = ES_GetNumTicketViews(tid, &nb_views); + if (ret < 0) + return ret; + + /* Allocate memory */ + views = (tikview *)memalign(32, sizeof(tikview) * nb_views); + if (!views) + return -1; + + /* Get ticket views */ + ret = ES_GetTicketViews(tid, views, nb_views); + if (ret < 0) + goto err; + + /* Set values */ + *outbuf = views; + *outlen = nb_views; + + return 0; + +err: + /* Free memory */ + if (views) + free(views); + + return ret; +} + +s32 Title_GetTMD(u64 tid, signed_blob **outbuf, u32 *outlen) +{ + void *p_tmd = NULL; + + u32 len; + s32 ret; + + /* Get TMD size */ + ret = ES_GetStoredTMDSize(tid, &len); + if (ret < 0) + return ret; + + /* Allocate memory */ + p_tmd = memalign(32, round_up(len, 32)); + if (!p_tmd) + return -1; + + /* Read TMD */ + ret = ES_GetStoredTMD(tid, p_tmd, len); + if (ret < 0) + goto err; + + /* Set values */ + *outbuf = p_tmd; + *outlen = len; + + return 0; + +err: + /* Free memory */ + if (p_tmd) + free(p_tmd); + + return ret; +} + +s32 Title_GetVersion(u64 tid, u16 *outbuf) +{ + signed_blob *p_tmd = NULL; + tmd *tmd_data = NULL; + + u32 len; + s32 ret; + + /* Get title TMD */ + ret = Title_GetTMD(tid, &p_tmd, &len); + if (ret < 0) + return ret; + + /* Retrieve TMD info */ + tmd_data = (tmd *)SIGNATURE_PAYLOAD(p_tmd); + + /* Set values */ + *outbuf = tmd_data->title_version; + + /* Free memory */ + free(p_tmd); + + return 0; +} + +s32 Title_GetSysVersion(u64 tid, u64 *outbuf) +{ + signed_blob *p_tmd = NULL; + tmd *tmd_data = NULL; + + u32 len; + s32 ret; + + /* Get title TMD */ + ret = Title_GetTMD(tid, &p_tmd, &len); + if (ret < 0) + return ret; + + /* Retrieve TMD info */ + tmd_data = (tmd *)SIGNATURE_PAYLOAD(p_tmd); + + /* Set values */ + *outbuf = tmd_data->sys_version; + + /* Free memory */ + free(p_tmd); + + return 0; +} + +s32 Title_GetSize(u64 tid, u32 *outbuf) +{ + signed_blob *p_tmd = NULL; + tmd *tmd_data = NULL; + + u32 cnt, len, size = 0; + s32 ret; + + /* Get title TMD */ + ret = Title_GetTMD(tid, &p_tmd, &len); + if (ret < 0) + return ret; + + /* Retrieve TMD info */ + tmd_data = (tmd *)SIGNATURE_PAYLOAD(p_tmd); + + /* Calculate title size */ + for (cnt = 0; cnt < tmd_data->num_contents; cnt++) { + tmd_content *content = &tmd_data->contents[cnt]; + + /* Add content size */ + size += content->size; + } + + /* Set values */ + *outbuf = size; + + /* Free memory */ + free(p_tmd); + + return 0; +} + +s32 Title_GetIOSVersions(u8 **outbuf, u32 *outlen) +{ + u8 *buffer = NULL; + u64 *list = NULL; + + u32 count, cnt, idx; + s32 ret; + + /* Get title list */ + ret = Title_GetList(&list, &count); + if (ret < 0) + return ret; + + /* Count IOS */ + for (cnt = idx = 0; idx < count; idx++) { + u32 tidh = (list[idx] >> 32); + u32 tidl = (list[idx] & 0xFFFFFFFF); + + /* Title is IOS */ + if ((tidh == 0x1) && (tidl >= 3) && (tidl <= 255)) + cnt++; + } + + /* Allocate memory */ + buffer = (u8 *)memalign(32, cnt); + if (!buffer) { + ret = -1; + goto out; + } + + /* Copy IOS */ + for (cnt = idx = 0; idx < count; idx++) { + u32 tidh = (list[idx] >> 32); + u32 tidl = (list[idx] & 0xFFFFFFFF); + + /* Title is IOS */ + if ((tidh == 0x1) && (tidl >= 3) && (tidl <= 255)) + buffer[cnt++] = (u8)(tidl & 0xFF); + } + + /* Set values */ + *outbuf = buffer; + *outlen = cnt; + + goto out; + +out: + /* Free memory */ + if (list) + free(list); + + return ret; +} diff --git a/nand-loader/source/title.h b/nand-loader/source/title.h new file mode 100644 index 0000000..5d1c573 --- /dev/null +++ b/nand-loader/source/title.h @@ -0,0 +1,16 @@ +#ifndef _TITLE_H_ +#define _TITLE_H_ + +/* Constants */ +#define BLOCK_SIZE 1024 + +/* Prototypes */ +s32 Title_GetList(u64 **, u32 *); +s32 Title_GetTicketViews(u64, tikview **, u32 *); +s32 Title_GetTMD(u64, signed_blob **, u32 *); +s32 Title_GetVersion(u64, u16 *); +s32 Title_GetSysVersion(u64, u64 *); +s32 Title_GetSize(u64, u32 *); +s32 Title_GetIOSVersions(u8 **, u32 *); + +#endif diff --git a/nand-loader/source/tools.c b/nand-loader/source/tools.c new file mode 100644 index 0000000..b9f8808 --- /dev/null +++ b/nand-loader/source/tools.c @@ -0,0 +1,252 @@ +/******************************************************************************* + * tools.c + * + * Copyright (c) 2009 The Lemon Man + * Copyright (c) 2009 Nicksasa + * Copyright (c) 2009 WiiPower + * + * Distributed under the terms of the GNU General Public License (v2) + * See http://www.gnu.org/licenses/gpl-2.0.txt for more info. + * + * Description: + * ----------- + * + ******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "tools.h" + +void printheadline() +{ + int rows, cols; + CON_GetMetrics(&cols, &rows); + + printf("TriiForce beta 7"); + + char buf[64]; + sprintf(buf, "IOS%u (Rev %u)\n", IOS_GetVersion(), IOS_GetRevision()); + printf("\x1B[%d;%dH", 0, cols-strlen(buf)-1); + printf(buf); +} + +void set_highlight(bool highlight) +{ + if (highlight) + { + printf("\x1b[%u;%um", 47, false); + printf("\x1b[%u;%um", 30, false); + } else + { + printf("\x1b[%u;%um", 37, false); + printf("\x1b[%u;%um", 40, false); + } +} + +void *allocate_memory(u32 size) +{ + return memalign(32, (size+31)&(~31) ); +} + +void Verify_Flags() +{ + if (Power_Flag) + { + WPAD_Shutdown(); + STM_ShutdownToStandby(); + } + if (Reset_Flag) + { + WPAD_Shutdown(); + STM_RebootSystem(); + } +} + + +void waitforbuttonpress(u32 *out, u32 *outGC) +{ + u32 pressed = 0; + u32 pressedGC = 0; + + while (true) + { + Verify_Flags(); + + WPAD_ScanPads(); + pressed = WPAD_ButtonsDown(0); + + PAD_ScanPads(); + pressedGC = PAD_ButtonsDown(0); + + if(pressed || pressedGC) + { + if (pressedGC) + { + // Without waiting you can't select anything + usleep (20000); + } + if (out) *out = pressed; + if (outGC) *outGC = pressedGC; + return; + } + } +} + + +s32 read_file(char *filepath, u8 **buffer, u32 *filesize) +{ + s32 Fd; + int ret; + + if (buffer == NULL) + { + printf("NULL Pointer\n"); + return -1; + } + + Fd = ISFS_Open(filepath, ISFS_OPEN_READ); + if (Fd < 0) + { + printf("ISFS_Open %s failed %d\n", filepath, Fd); + return Fd; + } + + fstats *status; + status = allocate_memory(sizeof(fstats)); + if (status == NULL) + { + printf("Out of memory for status\n"); + return -1; + } + + ret = ISFS_GetFileStats(Fd, status); + if (ret < 0) + { + printf("ISFS_GetFileStats failed %d\n", ret); + ISFS_Close(Fd); + free(status); + return -1; + } + + *buffer = allocate_memory(status->file_length); + if (*buffer == NULL) + { + printf("Out of memory for buffer\n"); + ISFS_Close(Fd); + free(status); + return -1; + } + + ret = ISFS_Read(Fd, *buffer, status->file_length); + if (ret < 0) + { + printf("ISFS_Read failed %d\n", ret); + ISFS_Close(Fd); + free(status); + free(*buffer); + return ret; + } + ISFS_Close(Fd); + + *filesize = status->file_length; + free(status); + + return 0; +} + +s32 identify(u64 titleid, u32 *ios) +{ + char filepath[ISFS_MAXPATH] ATTRIBUTE_ALIGN(0x20); + u8 *tmdBuffer = NULL; + u32 tmdSize; + u8 *tikBuffer = NULL; + u32 tikSize; + u8 *certBuffer = NULL; + u32 certSize; + + int ret; + + printf("Reading TMD..."); + fflush(stdout); + + sprintf(filepath, "/title/%08x/%08x/content/title.tmd", TITLE_UPPER(titleid), TITLE_LOWER(titleid)); + ret = read_file(filepath, &tmdBuffer, &tmdSize); + if (ret < 0) + { + printf("Reading TMD failed\n"); + return ret; + } + printf("done\n"); + + *ios = (u32)(tmdBuffer[0x18b]); + + printf("Reading ticket..."); + fflush(stdout); + + sprintf(filepath, "/ticket/%08x/%08x.tik", TITLE_UPPER(titleid), TITLE_LOWER(titleid)); + ret = read_file(filepath, &tikBuffer, &tikSize); + if (ret < 0) + { + printf("Reading ticket failed\n"); + free(tmdBuffer); + return ret; + } + printf("done\n"); + + printf("Reading certs..."); + fflush(stdout); + + sprintf(filepath, "/sys/cert.sys"); + ret = read_file(filepath, &certBuffer, &certSize); + if (ret < 0) + { + printf("Reading certs failed\n"); + free(tmdBuffer); + free(tikBuffer); + return ret; + } + printf("done\n"); + + printf("ES_Identify..."); + fflush(stdout); + + ret = ES_Identify((signed_blob*)certBuffer, certSize, (signed_blob*)tmdBuffer, tmdSize, (signed_blob*)tikBuffer, tikSize, NULL); + if (ret < 0) + { + switch(ret) + { + case ES_EINVAL: + printf("Error! ES_Identify (ret = %d;) Data invalid!\n", ret); + break; + case ES_EALIGN: + printf("Error! ES_Identify (ret = %d;) Data not aligned!\n", ret); + break; + case ES_ENOTINIT: + printf("Error! ES_Identify (ret = %d;) ES not initialized!\n", ret); + break; + case ES_ENOMEM: + printf("Error! ES_Identify (ret = %d;) No memory!\n", ret); + break; + default: + printf("Error! ES_Identify (ret = %d)\n", ret); + break; + } + free(tmdBuffer); + free(tikBuffer); + free(certBuffer); + return ret; + } + printf("done\n"); + + free(tmdBuffer); + free(tikBuffer); + free(certBuffer); + return 0; +} + + diff --git a/nand-loader/source/tools.h b/nand-loader/source/tools.h new file mode 100644 index 0000000..b7532cc --- /dev/null +++ b/nand-loader/source/tools.h @@ -0,0 +1,31 @@ +/******************************************************************************* + * tools.h + * + * Copyright (c) 2009 The Lemon Man + * Copyright (c) 2009 Nicksasa + * Copyright (c) 2009 WiiPower + * + * Distributed under the terms of the GNU General Public License (v2) + * See http://www.gnu.org/licenses/gpl-2.0.txt for more info. + * + * Description: + * ----------- + * + ******************************************************************************/ + +#define TITLE_UPPER(x) ((u32)((x) >> 32)) +#define TITLE_LOWER(x) ((u32)(x)) +#define TITLE_ID(x,y) (((u64)(x) << 32) | (y)) + +bool Power_Flag; +bool Reset_Flag; + +void *allocate_memory(u32 size); +s32 read_file(char *filepath, u8 **buffer, u32 *filesize); +s32 identify(u64 titleid, u32 *ios); +void set_highlight(bool highlight); +void waitforbuttonpress(u32 *out, u32 *outGC); +void printheadline(); + + + diff --git a/nand-loader/source/utils.h b/nand-loader/source/utils.h new file mode 100644 index 0000000..3a4862b --- /dev/null +++ b/nand-loader/source/utils.h @@ -0,0 +1,15 @@ +#ifndef _UTILS_H_ +#define _UTILS_H_ + +/* Constants */ +#define KB_SIZE 1024.0 +#define MB_SIZE 1048576.0 +#define GB_SIZE 1073741824.0 + +/* Macros */ +#define round_up(x,n) (-(-(x) & -(n))) + +/* Prototypes */ +u32 swap32(u32); + +#endif diff --git a/nand-loader/source/video.c b/nand-loader/source/video.c new file mode 100644 index 0000000..05ccd83 --- /dev/null +++ b/nand-loader/source/video.c @@ -0,0 +1,147 @@ +#include +#include + +#include "sys.h" +#include "video.h" + +/* Video variables */ +static void *framebuffer = NULL; +static GXRModeObj *vmode = NULL; + + +void Con_Init(u32 x, u32 y, u32 w, u32 h) +{ + /* Create console in the framebuffer */ + CON_InitEx(vmode, x, y, w, h); +} + +void Con_Destroy(void) +{ + /* Destroy console */ + VIDEO_SetPostRetraceCallback(NULL); +} + +void Con_Clear(void) +{ + /* Clear console */ + printf("\x1b[2J"); + fflush(stdout); +} + +void Con_ClearLine(void) +{ + s32 cols, rows; + u32 cnt; + + printf("\r"); + fflush(stdout); + + /* Get console metrics */ + CON_GetMetrics(&cols, &rows); + + /* Erase line */ + for (cnt = 1; cnt < cols; cnt++) { + printf(" "); + fflush(stdout); + } + + printf("\r"); + fflush(stdout); +} + +void Con_FgColor(u32 color, u8 bold) +{ + /* Set foreground color */ + printf("\x1b[%u;%um", color + 30, bold); + fflush(stdout); +} + +void Con_BgColor(u32 color, u8 bold) +{ + /* Set background color */ + printf("\x1b[%u;%um", color + 40, bold); + fflush(stdout); +} + +void Con_FillRow(u32 row, u32 color, u8 bold) +{ + s32 cols, rows; + u32 cnt; + + /* Set color */ + printf("\x1b[%u;%um", color + 40, bold); + fflush(stdout); + + /* Get console metrics */ + CON_GetMetrics(&cols, &rows); + + /* Save current row and col */ + printf("\x1b[s"); + fflush(stdout); + + /* Move to specified row */ + printf("\x1b[%u;0H", row); + fflush(stdout); + + /* Fill row */ + for (cnt = 0; cnt < cols; cnt++) { + printf(" "); + fflush(stdout); + } + + /* Load saved row and col */ + printf("\x1b[u"); + fflush(stdout); + + /* Set default color */ + Con_BgColor(0, 0); + Con_FgColor(7, 1); +} + +void Video_Configure(GXRModeObj *rmode) +{ + /* Configure the video subsystem */ + VIDEO_Configure(rmode); + + /* Setup video */ + VIDEO_SetBlack(FALSE); + VIDEO_Flush(); + VIDEO_WaitVSync(); + + if (rmode->viTVMode & VI_NON_INTERLACE) + VIDEO_WaitVSync(); +} + +void Video_SetMode(void) +{ + /* Select preferred video mode */ + vmode = VIDEO_GetPreferredMode(NULL); + + /* Allocate memory for the framebuffer */ + framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(vmode)); + + /* Configure the video subsystem */ + VIDEO_Configure(vmode); + + /* Setup video */ + VIDEO_SetNextFramebuffer(framebuffer); + VIDEO_SetBlack(FALSE); + VIDEO_Flush(); + VIDEO_WaitVSync(); + + if (vmode->viTVMode & VI_NON_INTERLACE) + VIDEO_WaitVSync(); + + /* Clear the screen */ + Video_Clear(COLOR_BLACK); +} + +void Video_Clear(s32 color) +{ + VIDEO_ClearFrameBuffer(vmode, framebuffer, color); +} + +void Video_DrawPng(IMGCTX ctx, PNGUPROP imgProp, u16 x, u16 y) +{ + PNGU_DECODE_TO_COORDS_YCbYCr(ctx, x, y, imgProp.imgWidth, imgProp.imgHeight, vmode->fbWidth, vmode->xfbHeight, framebuffer); +} diff --git a/nand-loader/source/video.h b/nand-loader/source/video.h new file mode 100644 index 0000000..87c2fa9 --- /dev/null +++ b/nand-loader/source/video.h @@ -0,0 +1,20 @@ +#ifndef _VIDEO_H_ +#define _VIDEO_H_ + +#include "pngu.h" + +/* Prototypes */ +void Con_Init(u32, u32, u32, u32); +void Con_Destroy(void); +void Con_Clear(void); +void Con_ClearLine(void); +void Con_FgColor(u32, u8); +void Con_BgColor(u32, u8); +void Con_FillRow(u32, u32, u8); + +void Video_Configure(GXRModeObj *); +void Video_SetMode(void); +void Video_Clear(s32); +void Video_DrawPng(IMGCTX, PNGUPROP, u16, u16); + +#endif diff --git a/nand-loader/source/wpad.c b/nand-loader/source/wpad.c new file mode 100644 index 0000000..8894fbb --- /dev/null +++ b/nand-loader/source/wpad.c @@ -0,0 +1,60 @@ +#include +#include + +#include "sys.h" +#include "wpad.h" + +/* Constants */ +#define MAX_WIIMOTES 4 + + +s32 Wpad_Init(void) +{ + s32 ret; + + /* Initialize Wiimote subsystem */ + ret = WPAD_Init(); + if (ret < 0) + return ret; + + return ret; +} + +void Wpad_Disconnect(void) +{ + u32 cnt; + + /* Disconnect Wiimotes */ + for (cnt = 0; cnt < MAX_WIIMOTES; cnt++) + WPAD_Disconnect(cnt); + + /* Shutdown Wiimote subsystem */ + WPAD_Shutdown(); +} + +u32 Wpad_GetButtons(void) +{ + u32 buttons = 0, cnt; + + /* Scan pads */ + WPAD_ScanPads(); + + /* Get pressed buttons */ + for (cnt = 0; cnt < MAX_WIIMOTES; cnt++) + buttons |= WPAD_ButtonsDown(cnt); + + return buttons; +} + +u32 Wpad_WaitButtons(void) +{ + u32 buttons = 0; + + /* Wait for button pressing */ + while (!buttons) { + buttons = Wpad_GetButtons(); + VIDEO_WaitVSync(); + } + + return buttons; +} diff --git a/nand-loader/source/wpad.h b/nand-loader/source/wpad.h new file mode 100644 index 0000000..63053bb --- /dev/null +++ b/nand-loader/source/wpad.h @@ -0,0 +1,12 @@ +#ifndef _WPAD_H_ +#define _WPAD_H_ + +#include + +/* Prototypes */ +s32 Wpad_Init(void); +void Wpad_Disconnect(void); +u32 Wpad_GetButtons(void); +u32 Wpad_WaitButtons(void); + +#endif diff --git a/nand-loader/stub/Makefile b/nand-loader/stub/Makefile deleted file mode 100644 index 78d4683..0000000 --- a/nand-loader/stub/Makefile +++ /dev/null @@ -1,42 +0,0 @@ - -PREFIX = powerpc-eabi- - -AR = $(PREFIX)ar -AS = $(PREFIX)as -CC = $(PREFIX)gcc -CXX = $(PREFIX)g++ -LD = $(PREFIX)ld -OBJCOPY = $(PREFIX)objcopy -RANLIB = $(PREFIX)ranlib -STRIP = $(PREFIX)strip - -MACHDEP = -mcpu=750 -mno-eabi -mhard-float -LDFLAGS = $(MACHDEP) -n -nostartfiles -nostdlib -Wl,-T,openstub.ld -L. -ASFLAGS = -D_LANGUAGE_ASSEMBLY -DHW_RVL - -TARGET_LINKED = stub.elf -TARGET = stub.bin -OBJS = crt0.o - -DEPDIR = .deps - -all: $(TARGET) - -%.o: %.s - @echo " ASSEMBLE $<" - @$(CC) $(ASFLAGS) -c $< -o $@ - -%.o: %.S - @echo " ASSEMBLE $<" - @$(CC) $(ASFLAGS) -c $< -o $@ - -$(TARGET_LINKED): $(OBJS) - @echo " LINK $@" - @$(CC) -g -o $@ $(LDFLAGS) $(OBJS) - -$(TARGET): $(TARGET_LINKED) - @echo " OBJCOPY $@" - @$(OBJCOPY) -O binary $< $@ - -clean: - rm -rf $(TARGET_LINKED) $(TARGET) $(OBJS) $(DEPDIR) diff --git a/nand-loader/stub/crt0.S b/nand-loader/stub/crt0.S deleted file mode 100644 index 635b6e0..0000000 --- a/nand-loader/stub/crt0.S +++ /dev/null @@ -1,123 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt - -# This code comes from the Twilight Hack -# Copyright 2008-2009 Segher Boessenkool - -# Slightly modified to imitate the homebrew channel stub - -*/ -.set r0,0; .set r1,1; .set r2,2; .set r3,3; .set r4,4; -.set r5,5; .set r6,6; .set r7,7; .set r8,8; .set r9,9; -.set r10,10; .set r11,11; .set r12,12; .set r13,13; .set r14,14; -.set r15,15; .set r16,16; .set r17,17; .set r18,18; .set r19,19; -.set r20,20; .set r21,21; .set r22,22; .set r23,23; .set r24,24; -.set r25,25; .set r26,26; .set r27,27; .set r28,28; .set r29,29; -.set r30,30; .set r31,31; - -#include "hw.h" - - .globl _start -_start: - mfmsr r3 - rlwinm r4,r3,0,17,15 # MSR_EE - rlwinm r4,r4,0,26,24 # MSR_IP - mtmsr r4 - isync - lis r3,_setup@h - ori r3,r3,_setup@l - clrlwi r3,r3,2 - mtsrr0 r3 - mfmsr r3 - li r4,MSR_IR|MSR_DR - andc r3,r3,r4 - mtsrr1 r3 - rfi - -#0001 0001 0000 1100 0110 0100 -#BHT,BTIC,DCFA,DCFI,ICFI,NHR,DPM -_setup: - lis r3,0x11 - ori r3,r3,0xC64 #0x110C64 - mtspr rHID0,r3 - isync - li r4,MSR_FP - mtmsr r4 - ori r3,r3,HID0_ICE|HID0_DCE - mtspr rHID0,r3 - isync - li r0,0 - mtibatu 0,r0 - mtibatu 1,r0 - mtibatu 2,r0 - mtibatu 3,r0 - mtdbatu 0,r0 - mtdbatu 1,r0 - mtdbatu 2,r0 - mtdbatu 3,r0 - #mtibatl 0,r0 - mtspr 560,r0 - mtspr 562,r0 - mtspr 564,r0 - mtspr 566,r0 - mtspr 568,r0 - mtspr 570,r0 - mtspr 572,r0 - mtspr 574,r0 - isync - lis r0,0x8000 - mtsr 0,r0 - mtsr 1,r0 - mtsr 2,r0 - mtsr 3,r0 - mtsr 4,r0 - mtsr 5,r0 - mtsr 6,r0 - mtsr 7,r0 - mtsr 8,r0 - mtsr 9,r0 - mtsr 10,r0 - mtsr 11,r0 - mtsr 12,r0 - mtsr 13,r0 - mtsr 14,r0 - mtsr 15,r0 - isync - li r3,2 #0x00000000|PP=2 - lis r4,0x8000 - ori r4,r4,0x1FFF #0x80000000|256Mbytes|VS|VP - mtibatl 0,r3 - mtibatu 0,r4 - mtdbatl 0,r3 - mtdbatu 0,r4 - isync - addis r3,r3,0x1000 #0x10000000|PP=2 - addis r4,r4,0x1000 #0x90000000|256Mbytes|VS|VP - mtspr 561,r3 - mtspr 560,r4 - mtspr 569,r3 - mtspr 568,r4 - isync - li r3,0x2A #0x00000000|I|G|PP=2 - lis r4,0xC000 - ori r4,r4,0x1FFF #0xC0000000|256Mbytes|VS|VP - mtdbatu 1,r3 - mtdbatu 1,r4 - isync - addis r3,r3,0x1000 #0x10000000|I|G|PP=2 - addis r4,r4,0x1000 #0xD0000000|256Mbytes|VS|VP - mtspr 571,r3 - mtspr 570,r4 - isync - lis r3,0x8200 - mtspr 1011,r3 - lis r3,0x8000 - ori r3,r3,0x4000 - mtsrr0 r3 - mfmsr r3 - ori r3,r3,MSR_DR|MSR_IR - mtsrr1 r3 - rfi diff --git a/nand-loader/stub/hw.h b/nand-loader/stub/hw.h deleted file mode 100644 index f4486ba..0000000 --- a/nand-loader/stub/hw.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef __HW_H__ -#define __HW_H__ - -#define MSR_POW (1<<18) -#define MSR_ILE (1<<16) -#define MSR_EE (1<<15) -#define MSR_PR (1<<14) -#define MSR_FP (1<<13) -#define MSR_ME (1<<12) -#define MSR_FE0 (1<<11) -#define MSR_SE (1<<10) -#define MSR_BE (1<< 9) -#define MSR_FE1 (1<< 8) -#define MSR_IP (1<< 6) -#define MSR_IR (1<< 5) -#define MSR_DR (1<< 4) -#define MSR_RI (1<< 1) -#define MSR_LE (1<< 0) - -#define HID0_EMCP (1<<31) -#define HID0_DBP (1<<30) -#define HID0_EBA (1<<29) -#define HID0_EBD (1<<28) -#define HID0_BCLK (1<<27) -#define HID0_ECLK (1<<25) -#define HID0_PAR (1<<24) -#define HID0_DOZE (1<<23) -#define HID0_NAP (1<<22) -#define HID0_SLEEP (1<<21) -#define HID0_DPM (1<<20) -#define HID0_NHR (1<<16) -#define HID0_ICE (1<<15) -#define HID0_DCE (1<<14) -#define HID0_ILOCK (1<<13) -#define HID0_DLOCK (1<<12) -#define HID0_ICFI (1<<11) -#define HID0_DCFI (1<<10) -#define HID0_SPD (1<< 9) -#define HID0_IFEM (1<< 8) -#define HID0_SGE (1<< 7) -#define HID0_DCFA (1<< 6) -#define HID0_BTIC (1<< 5) -#define HID0_ABE (1<< 3) -#define HID0_BHT (1<< 2) -#define HID0_NOOPTI (1<< 0) - -#define HID2_LSQE (1<<31) -#define HID2_WPE (1<<30) -#define HID2_PSE (1<<29) -#define HID2_LCE (1<<28) - -#define L2CR_L2E (1<<31) -#define L2CR_L2CE (1<<30) -#define L2CR_L2DO (1<<22) -#define L2CR_L2I (1<<21) -#define L2CR_L2WT (1<<19) -#define L2CR_L2TS (1<<18) -#define L2CR_L2IP (1<< 0) - -#define DMAU_MEM_ADDR_MASK 0xFFFFFFE0 -#define DMAU_LENU(x) (x & 0x1F) -#define DMAL_LC_ADDR_MASK 0xFFFFFFE0 -#define DMAL_LD (1<< 4) -#define DMAL_LENL(x) (x & 0xC) -#define DMAL_T (1<< 1) -#define DMAL_F (1<< 0) - -#define BATU_BEPI_MASK 0xFFFC0000 -#define BATU_BL(x) (x & 0x00001FFC) -#define BATU_VS (1<< 1) -#define BATU_VP (1<< 0) -#define BATL_BRPN_MASK 0xFFFC0000 -#define BATL_WIMG_MASK 0x78 -#define BATL_PP (1<< 0) - -// BATU - 0x80001FFF == 256Mbytes -// 1000 0000 000x xxx0 0001 1111 1111 11xx -// 0x80000000|256Mbytes|VS|VP -// BATL - 0x00000002 -// 0000 0000 0000 000x xxxx xxxx x000 0x10 -// PP=b10 -// -// BATU - 0xC0001FFF == 256Mbytes -// BATL - 0x0000002a -// 0000 0000 0000 000x xxxx xxxx x010 1x10 -// WIMG=b0101|PP=b10 -// - -#define rHID2 920 -#define rDMAU 922 -#define rDMAL 923 -#define rHID0 1008 -#define rHID1 1009 -#define rHID4 1011 - -/* - * Upper PTE - * 0|1-24|25|26-31 - * V|VSID|H |API - * - * Lower PTE - * 0-19|20-22|23|24|25-28|29|30-31 - * RPN |000 |R |C |WIMG |0 |PP -*/ - -#endif - diff --git a/nand-loader/stub/openstub.ld b/nand-loader/stub/openstub.ld deleted file mode 100644 index fd3fc8b..0000000 --- a/nand-loader/stub/openstub.ld +++ /dev/null @@ -1,19 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -OUTPUT_FORMAT("elf32-powerpc") -OUTPUT_ARCH(powerpc:common) - -ENTRY(_start) - -PHDRS { - app PT_LOAD FLAGS(7); -} - -SECTIONS { - .text : { *(.text) *(.text.*) } :app -} diff --git a/nand-loader/types.h b/nand-loader/types.h deleted file mode 100644 index ea71e6e..0000000 --- a/nand-loader/types.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -// Copyright 2008-2009 Hector Martin - -#ifndef __TYPES_H__ -#define __TYPES_H__ - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned long long u64; -typedef char s8; -typedef short s16; -typedef int s32; -typedef long long s64; - -typedef volatile unsigned char vu8; -typedef volatile unsigned short vu16; -typedef volatile unsigned int vu32; -typedef volatile unsigned long long vu64; - -#define NULL ((void *)0) - -#define ALIGNED(n) __attribute__((aligned(n))) - -#endif - diff --git a/nand-loader/utils.c b/nand-loader/utils.c deleted file mode 100644 index 940cec6..0000000 --- a/nand-loader/utils.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -/* This code comes from HBC's stub which was based on geckoloader and the Twilight Hack code */ -/* Some of these routines are from public domain sources */ -// Copyright 2008-2009 Segher Boessenkool -// Copyright 2008-2009 Andre Heider -// Copyright 2008-2009 Hector Martin - -#include "types.h" -#include "utils.h" - -static u32 -get_time(void) -{ - u32 x; - - asm volatile("mftb %0" : "=r"(x)); - - return x; -} - -void -usleep(u32 us) -{ - u32 _start = get_time(); - while ((get_time() - _start) < (91*us)) ; -} - diff --git a/nand-loader/utils.h b/nand-loader/utils.h deleted file mode 100644 index 9424a26..0000000 --- a/nand-loader/utils.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - TinyLoad - a simple region free (original) game launcher in 4k - -# This code is licensed to you under the terms of the GNU GPL, version 2; -# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt -*/ - -// Copyright 2008-2009 Hector Martin - -#ifndef __UTILS_H__ -#define __UTILS_H__ - -#include "types.h" - -void usleep(u32 us); - -#endif