mirror of
https://github.com/Wack0/entii-for-workcubes.git
synced 2025-12-19 18:06:24 -05:00
Initial commit
This commit is contained in:
340
COPYING
Normal file
340
COPYING
Normal file
@@ -0,0 +1,340 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
51 Franklin St, 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 Library 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.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
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 St, 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.
|
||||
|
||||
<signature of Ty Coon>, 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 Library General
|
||||
Public License instead of this License.
|
||||
137
README.md
Normal file
137
README.md
Normal file
@@ -0,0 +1,137 @@
|
||||
|
||||
[](https://discord.gg/S8jWsc4SAq)
|
||||
|
||||
# Windows NT for GameCube/Wii
|
||||
|
||||
The following systems are supported:
|
||||
* Nintendo GameCube
|
||||
* Nintendo Wii
|
||||
* Wii Mini requires SD card hardmod (for now)
|
||||
* Nintendo Wii U (**vWii only for now**)
|
||||
|
||||
The following systems are theoretically supported, although not tested due to the rarity of such hardware:
|
||||
* Broadway Evaluation Board
|
||||
* Cortado boards
|
||||
|
||||
The following systems will NEVER be supported:
|
||||
* early Dolphin Development Hardware with only 4MB of usable RAM
|
||||
|
||||
## Drivers present
|
||||
|
||||
* Flipper interrupt controller (in HAL)
|
||||
* Flipper Video Interface console framebuffer (YUV XFB) for ARC firmware and HAL
|
||||
* Flipper GPU RGB framebuffer under NT (writing to EFB under text setup, writing to texture under GDI; copying out to XFB on vblank interrupt)
|
||||
* Flipper Serial Interface (gamecube controller ports), supporting the following devices:
|
||||
* GameCube ASCII keyboard controller, plus unreleased English/European variants (discovered through reversing Phantasy Star Online); the latter are completely untested, the former has not been tested on real hardware
|
||||
* GameCube controller, with the following mappings:
|
||||
* Under ARC firmware: left analog stick and d-pad maps to up/down, A button maps to enter, B button maps to escape, X button maps to letter 'S'
|
||||
* Under NT text setup: left analog stick and d-pad maps to up/down, c-stick maps to page up/page down, A button maps to enter, B button maps to escape, X button maps to F8, Y button maps to letter 'C', Z button maps to letter 'L'
|
||||
* Under NT GDI: left analog stick moves mouse, A button maps to left mouse button, B button maps to right mouse button, L+R together maps to ctrl+alt+del, c-stick allows for choosing a keyboard scancode (1-9, 0, a-z), X button confirms the selected scancode. Numbers are first in the list so numeric-only text boxes (like entering CD key) still works.
|
||||
* N64 Randnet keyboard, completely untested so may have issues
|
||||
* N64 mouse (under NT only), completely untested so may have issues
|
||||
* N64 controller (completely untested so may have issues), with the following mappings:
|
||||
* Under ARC firmware: left analog stick and d-pad maps to up/down, A button maps to enter, B button maps to escape, Z button maps to letter 'S'
|
||||
* Under NT text setup: left analog stick and d-pad maps to up/down, c-stick maps to page up/page down, A button maps to enter, B button maps to escape, Z button maps to F8, L trigger maps to letter 'C', R trigger maps to letter 'L'
|
||||
* Under NT GDI: left analog stick moves mouse, A button maps to left mouse button, B button maps to right mouse button, L+R together maps to ctrl+alt+del, c-down and c-up allows for choosing a keyboard scancode (1-9, 0, a-z), start button confirms the selected scancode. Numbers are first in the list so numeric-only text boxes (like entering CD key) still works.
|
||||
* Flipper External Interface (SPI bus), supporting the following devices:
|
||||
* RTC
|
||||
* USB Gecko (for kernel debugger only)
|
||||
* SD Gecko or compatible
|
||||
* IDE-EXI or compatible (has not been tested on real hardware)
|
||||
* Vegas IOP IPC
|
||||
* Vegas SDMC controller (via IOS)
|
||||
* Vegas USB (OHCI/EHCI) controllers (via IOS), supporting the following devices:
|
||||
* USB keyboard
|
||||
* USB mouse
|
||||
* USB mass storage (currently has some issues, some devices may not work)
|
||||
* **Hotplugging USB devices is not supported. To use a USB device, it must be plugged in before launching the ARC firmware.**
|
||||
|
||||
## Software compatibility
|
||||
|
||||
NT 3.51 RTM and higher. NT 3.51 betas (build 944 and below) will need kernel patches to run due to processor detection bugs. NT 3.5 will never be compatible, as it only supports PowerPC 601.
|
||||
(The additional suspend/hibernation features in NT 3.51 PMZ could be made compatible in theory but in practise would require all of the additional drivers for that to be reimplemented.)
|
||||
|
||||
## Installing
|
||||
|
||||
### Preliminary
|
||||
|
||||
* Grab binaries from the release page, extract to SD card (or EXI-IDE device)
|
||||
* Copy an NT 3.51 or 4.0 ISO to `sd:\nt\disk00.iso`
|
||||
* Create a raw disk image of the size you want at `sd:\nt\disk00.img` - I use `qemu-img create disk00.img 2G`, change the size as appropriate. Remember that the maximum file size on a FAT32 partition is 4GB.
|
||||
* On a GameCube, load `arcldr_dol.dol` from Swiss; on Wii/vWii, load `arcldr` from the Homebrew Channel.
|
||||
|
||||
### Partitioning Disk
|
||||
|
||||
* When you get to ARC firmware menu, go to `Run firmware setup`, then `Repartition disk or disk image for NT installation`.
|
||||
* Select the disk image you created earlier.
|
||||
* Confirm the partition operation with Y (on keyboard), X button (on GameCube controller), or Z button (on N64 controller)
|
||||
* When finished, the partitioner will ask to `Press any key to restart`. This should either restart your system or return to loader where you can load `arcldr` again.
|
||||
|
||||
### Installing NT
|
||||
|
||||
* Choose `Run NT setup from cd00`.
|
||||
* You will receive the message `Setup could not determine the type of computer you have`.
|
||||
* Choose `Other` (default selected option), just press `Enter` (or A button) when asked for hardware support disk.
|
||||
* Choose the HAL from the list, currently there is only one option: `Nintendo GameCube, Wii and Wii U (vWii)`.
|
||||
* Next you will receive the message `Setup could not determine the type of one or more mass storage drivers installed in your system`. At least two drivers need to be loaded at this point.
|
||||
* To load a driver, press `S` (X button on GameCube controller, Z button on N64 controller) to pick a driver, choose `Other` from the list, press `Enter` (A button) when asked for hardware support disk, and choose the driver.
|
||||
* `Nintendo Wii SD Slot (via IOS) [Disk Images]` is required when using the front SD card slot on a Wii or Wii U
|
||||
* `Nintendo Wii USB (via IOS)` is required when using any USB device (keyboard, mouse or mass storage) on a Wii or Wii U
|
||||
* `Nintendo GameCube Controller Ports` is required when using devices plugged into the GameCube controller ports on a GameCube or Wii
|
||||
* `SD Gecko or IDE-EXI and Compatible [Disk Images]` is required when using SD Gecko (or compatible) or IDE-EXI (or compatible) devices in the GameCube memory card slots on a GameCube or Wii, or the serial ports present underneath a GameCube
|
||||
* To make this simpler: on a GameCube you will need only the last two; on a Wii U vWii you will only need the first two, and on a Wii you will need the first two and possibly the last two depending on if you are using/want to use the GameCube controller ports/memory card slots or not.
|
||||
* You will receive the message `Setup could not determine the type of video adapter installed in the system`. Choose `Other` from the list, press `Enter` when asked for hardware support disk, and choose the correct option depending on the OS you are installing.
|
||||
* There are two options in this list; `ArtX Flipper, ATI Vegas, AMD Bollywood (NT 4)` is for NT 4, `ArtX Flipper, ATI Vegas, AMD Bollywood (NT 3.x)` is for NT 3.51.
|
||||
* NT will boot and text setup will start. Go through the text setup.
|
||||
* Under `Setup has determined that your computer contains the following hardware and software components`, change `Keyboard` from `Unknown` to `XT, AT or Enhanced Keyboard (83-104 keys)` and `Pointing Device` from `Unknown` to `No Mouse or Other Pointing Device`.
|
||||
* Choose the `C:` drive from the partition list. If you chose to create an NT partition of size 2GB or less, it must be formatted.
|
||||
* If you chose to create an NT partition of over 2GB in size, errors will be found by the disk examination process which will require a reboot. You will need to boot back into the ARC firmware from Swiss or the Homebrew Channel and follow the "Installing NT" steps again to get back to this point.
|
||||
* On the second attempt, disk examination will succeed, so just choose the `C:` partition again in the NT text setup partition selector.
|
||||
* Proceed through the rest of NT text and graphical setup as normal.
|
||||
|
||||
## Known issues
|
||||
|
||||
* System may hang on reboot sometimes.
|
||||
* There are issues with some USB mass storage devices.
|
||||
* GDI driver uses slow unoptimised code for copying from GDI bitmap buffer to GPU texture buffer.
|
||||
* ARC firmware and NT drivers support exFAT for disk images on an SD card/EXI-IDE device, but the loader currently does not support exFAT for loading the ARC firmware proper.
|
||||
* The loader currently does not support loading the ARC firmware from a USB mass storage device.
|
||||
* Be aware that the EXI bus is slower compared to other disk interfaces, so using SD Gecko/EXI-IDE causes slowdowns. This is most notable when installing NT on GameCube where this is the only available option.
|
||||
|
||||
## Building ARC firmware
|
||||
|
||||
You need devkitPPC. Additionally, a `libgcc.a` compiled for `powerpcle` must be present in `arcfw/gccle`. If you need to find one, it should be present on any Void Linux mirror, the current filename to search for as of 2024-07-12 is `cross-powerpcle-linux-gnu-0.34_1.x86_64.xbps` - decompress it by `zstdcat cross-powerpcle-linux-gnu-0.34_1.x86_64.xbps -o cross-powerpcle-linux-gnu-0.34_1.x86_64.tar`, then pull the file out of the tarball: `usr/lib/gcc/powerpcle-linux-gnu/10.2/libgcc.a`.
|
||||
|
||||
* Ensure `DEVKITPPC` environment variable is set to your devkitPPC directory, usually `/opt/devkitpro/devkitPPC`
|
||||
* Build the ARC firmware loader: `cd arcldr ; make -f Makefile.rvl ; make -f Makefile.dol ; cd ..`
|
||||
* Build the little endian libc: `cd arcfw/baselibc ; make ; cd ../..`
|
||||
* Build the ARC firmware itself: `cd arcfw; make ; cd ..`
|
||||
|
||||
## Building HAL/drivers
|
||||
|
||||
You need [peppc](https://github.com/Wack0/peppc). Additionally, the powerpc libs from the [NT4 DDK](https://archive.org/details/94396011997WindowsNTDDKForWinNT4.0WorkstationUS.iso.7z) (`ddk/lib/ppc/free/*.lib`) must be present in `lib`. The rest of the toolchain (VC6 PPC CE cross compiler used for the C preprocessor for asm, as multi-line defines are handled improperly by gcc cpp; assembler PASM.EXE with single branch patched to skip "dump statements"; resource compiler and linker from MSVC 4.2, and its dependencies; `SPLITSYM.EXE` from NT 3.51 DDK to split COFF debug symbols from executables) is present in `msvc-ppc` directory.
|
||||
|
||||
The headers are included and come from various places with slight modifications for working with this toolchain, or for backwards compatibility reasons:
|
||||
* `nt4/sdk` - NT4 SDK
|
||||
* `nt4/ddk` - NT4 DDK (including all the headers from the `src/*/inc` directories)
|
||||
* `nt4/crt` - VC++ 4.0 (CRT headers)
|
||||
* `nt4/hal` - because of a lack of a public dump, this folder includes the headers that have evidence suggesting they were included in the NT4 halkit (minus `nthal.h` which is in the hal source folder, and was modified to allow for backwards compatibility). Some have been modified to allow them to be included by drivers after `ntddk.h` (so drivers can call `HalDisplayString` for debugging purposes, or use `LOADER_PARAMETER_BLOCK` to determine whether they are running in text setup or not).
|
||||
|
||||
The makefiles used are derived from devkitPro.
|
||||
|
||||
Ensure `PEPPC` environment variable is set to the `peppc-build/toolchain/bin` directory.
|
||||
|
||||
You must build the hal first (`cd halartx; make; cd ..`) before you can build the other drivers, as the HAL implements the exported IOP IPC and EXI drivers (due to the HAL itself using them).
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
* libc used is [baselibc](https://github.com/PetteriAimonen/Baselibc)
|
||||
* ELF loader, arcfw makefile (and some cache invalidation functions) adapted from [The Homebrew Channel](https://github.com/fail0verflow/hbc)
|
||||
* Other makefiles adapted from [devkitPro](https://github.com/devkitPro/devkitppc-rules)
|
||||
* Some lowlevel powerpc stuff, and ARC firmware framebuffer console implementation and font, adapted from [libogc](https://github.com/devkitPro/libogc)
|
||||
* EXI-IDE driver in ARC loader adapted from [Swiss](https://github.com/emukidid/swiss-gc/blob/master/cube/swiss/source/devices/fat/ata.c)
|
||||
* IOS IPC driver in ARC firmware adapted from [The Homebrew Channel's reload stub](https://github.com/fail0verflow/hbc/blob/master/channel/channelapp/stub/ios.c)
|
||||
* ISO9660 FS implementation inside ARC firmware is [lib9660](https://github.com/erincandescent/lib9660) with some modifications.
|
||||
* FAT FS implementation inside ARC firmware is [Petit FatFs](http://elm-chan.org/fsw/ff/00index_p.html) with some modifications; additionally the full [FatFs](http://elm-chan.org/fsw/ff/) is used for reading the underlying disk images on FAT16/FAT32/exFAT partitions (in ARC and inside iossdmc.sys and fpexiblk.sys)
|
||||
* GDI driver derived from NT4 DDK example `framebuf`.
|
||||
* Various drivers adapted from those in libogc.
|
||||
82
arcfw/Makefile
Normal file
82
arcfw/Makefile
Normal file
@@ -0,0 +1,82 @@
|
||||
ifeq ($(strip $(DEVKITPPC)),)
|
||||
$(error "Set DEVKITPPC in your environment.")
|
||||
endif
|
||||
|
||||
PREFIX = $(DEVKITPPC)/bin/powerpc-eabi-
|
||||
|
||||
CFLAGS = -mcpu=750 -m32 -mlittle -mhard-float -mno-eabi -mno-sdata
|
||||
CFLAGS += -ffreestanding -ffunction-sections -fdata-sections
|
||||
CFLAGS += -Ibaselibc/include
|
||||
CFLAGS += -Wall -Wextra -O2 -g
|
||||
ASFLAGS =
|
||||
LDFLAGS = -mcpu=750 -m32 -mlittle -n -nostartfiles -nodefaultlibs -Wl,-gc-sections
|
||||
|
||||
DEFINES = -DWITH_STDIO -DBASELIBC_INTERNAL -D__BIG_ENDIAN_SYSTEM__=1
|
||||
LDFLAGS += -nostartfiles -nodefaultlibs -L./baselibc -L./gccle
|
||||
|
||||
LDSCRIPT = of.ld
|
||||
TARGET = arcfw.elf
|
||||
|
||||
FILES = $(wildcard source/*.S) $(wildcard source/*.c) $(wildcard source/fatfs/*.c)
|
||||
OBJSx1 = $(FILES:source/%.S=build/%.o)
|
||||
OBJSx = $(OBJSx1:source/%.c=build/%.o)
|
||||
OBJS = $(OBJSx:build/fatfs/%.o=build/%.o)
|
||||
LIBS = -lcbase -lgcc
|
||||
|
||||
AR = $(PREFIX)ar
|
||||
AS = $(PREFIX)as
|
||||
CC = $(PREFIX)gcc
|
||||
CXX = $(PREFIX)g++
|
||||
LD = $(PREFIX)ld
|
||||
OBJCOPY = $(PREFIX)objcopy
|
||||
RANLIB = $(PREFIX)ranlib
|
||||
STRIP = $(PREFIX)strip
|
||||
|
||||
ifeq ($(NOMAPFILE),)
|
||||
LDFLAGS += -Wl,-Map,$(TARGET).map
|
||||
endif
|
||||
|
||||
ifneq ($(LDSCRIPT),)
|
||||
LDFLAGS += -Wl,-T$(LDSCRIPT)
|
||||
endif
|
||||
|
||||
DEPDIR = .deps
|
||||
|
||||
all: $(TARGET)
|
||||
@[ -d $(DIR_BUILD) ] || mkdir $(DIR_BUILD)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
@echo " LINK $@"
|
||||
@$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
|
||||
@cp $(TARGET) $(TARGET:%.elf=%_unstripped.elf)
|
||||
@$(STRIP) $(TARGET)
|
||||
|
||||
ifneq ($(LDSCRIPT),)
|
||||
$(TARGET): $(LDSCRIPT)
|
||||
endif
|
||||
|
||||
build/%.o: source/%.c
|
||||
@echo " COMPILE $<"
|
||||
@mkdir -p $(DEPDIR)
|
||||
@$(CC) $(CFLAGS) $(DEFINES) -Wp,-MMD,$(DEPDIR)/$(*F).d,-MQ,"$@",-MP -c $< -o $@
|
||||
|
||||
build/%.o: source/fatfs/%.c
|
||||
@echo " COMPILE $<"
|
||||
@mkdir -p $(DEPDIR)
|
||||
@$(CC) $(CFLAGS) $(DEFINES) -Wp,-MMD,$(DEPDIR)/$(*F).d,-MQ,"$@",-MP -c $< -o $@
|
||||
|
||||
build/%.o: source/%.s
|
||||
@echo " ASSEMBLE $<"
|
||||
@$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@
|
||||
|
||||
build/%.o: source/%.S
|
||||
@echo " ASSEMBLE $<"
|
||||
@$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(DEPDIR)
|
||||
rm -f $(TARGET) $(TARGET:%.elf=%_unstripped.elf) $(TARGET).map $(OBJS)
|
||||
|
||||
-include $(DEPDIR)/*
|
||||
|
||||
.PHONY: clean
|
||||
133
arcfw/baselibc/LICENSE
Normal file
133
arcfw/baselibc/LICENSE
Normal file
@@ -0,0 +1,133 @@
|
||||
Baselibc is based on klibc 1.5.23 and tinyprintf modules.
|
||||
None of the GPL-licensed parts of klibc are used.
|
||||
|
||||
Baselibc is licensed under the BSD license:
|
||||
|
||||
Copyright (c) 2012 Petteri Aimonen <jpa at blc.mail.kapsi.fi>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of Kustaa Nyholm or SpareTimeLabs nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The original licenses of the modules are included below:
|
||||
|
||||
------------------ Tinyprintf license ------------------
|
||||
|
||||
Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of Kustaa Nyholm or SpareTimeLabs nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
------------------- klibc license -------------------------
|
||||
This license applies to all files in directory and its subdirectories,
|
||||
unless otherwise noted in individual files.
|
||||
|
||||
|
||||
Some files are derived from files derived from the include/ directory
|
||||
of the Linux kernel, and are licensed under the terms of the GNU
|
||||
General Public License, version 2, as released by the Free Software
|
||||
Foundation, Inc.; incorporated herein by reference.
|
||||
[These files are not included in the baselibc.]
|
||||
|
||||
-----
|
||||
|
||||
Some files are derived from files copyrighted by the Regents of The
|
||||
University of California, and are available under the following
|
||||
license:
|
||||
|
||||
Note: The advertising clause in the license appearing on BSD Unix
|
||||
files was officially rescinded by the Director of the Office of
|
||||
Technology Licensing of the University of California on July 22
|
||||
1999. He states that clause 3 is "hereby deleted in its entirety."
|
||||
|
||||
* Copyright (c)
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
|
||||
For all remaining files [of klibc], the following license applies:
|
||||
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* Any copyright notice(s) and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
62
arcfw/baselibc/Makefile
Normal file
62
arcfw/baselibc/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
ifeq ($(strip $(DEVKITPPC)),)
|
||||
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
|
||||
endif
|
||||
|
||||
# You can override the CFLAGS and C compiler externally,
|
||||
# e.g. make PLATFORM=cortex-m3
|
||||
CFLAGS += -g -Os -Wall -Werror -I include -DBASELIBC_INTERNAL -DWITH_MALLOC -DWITH_STDIO -DPRINTF_LONG_SUPPORT
|
||||
|
||||
PLATFORM ?= ppc
|
||||
|
||||
ifeq ($(PLATFORM),cortex-m3)
|
||||
CFLAGS += -fno-common -Os
|
||||
CC = arm-none-eabi-gcc
|
||||
AR = arm-none-eabi-ar
|
||||
CFLAGS += -mcpu=cortex-m3 -mthumb
|
||||
endif
|
||||
ifeq ($(PLATFORM),mipsel)
|
||||
CC = ../2019.02-05/bin/mips-img-elf-gcc
|
||||
AR = ../2019.02-05/bin/mips-img-elf-gcc-ar
|
||||
CFLAGS += -G0 -mno-gpopt -march=r4000 -Os -mel -mgp32 -mfp32 -mabi=32
|
||||
endif
|
||||
ifeq ($(PLATFORM),ppc)
|
||||
CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc
|
||||
AR = $(DEVKITPPC)/bin/powerpc-eabi-gcc-ar
|
||||
CFLAGS += -mcpu=750 -m32 -mhard-float -mno-eabi -mno-sdata -mlittle
|
||||
endif
|
||||
|
||||
# With this, the makefile should work on Windows also.
|
||||
ifdef windir
|
||||
RM = del
|
||||
endif
|
||||
|
||||
# Just include all the source files in the build.
|
||||
CSRC = $(wildcard src/*.c)
|
||||
OBJS = $(CSRC:.c=.o)
|
||||
|
||||
# And the files for the test suite
|
||||
TESTS_CSRC = $(wildcard tests/*_tests.c)
|
||||
TESTS_OBJS = $(TESTS_CSRC:.c=)
|
||||
|
||||
# Some of the files uses "templates", i.e. common pieces
|
||||
# of code included from multiple files.
|
||||
CFLAGS += -Isrc/templates
|
||||
|
||||
all: libcbase.a
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(TESTS_OBJS) libcbase.a
|
||||
|
||||
libcbase.a: $(OBJS)
|
||||
$(RM) $@
|
||||
$(AR) ru $@ $^
|
||||
|
||||
run_tests: $(TESTS_OBJS)
|
||||
$(foreach f,$^,$f)
|
||||
|
||||
tests/%: tests/%.c tests/tests_glue.c libcbase.a
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
6
arcfw/baselibc/README.md
Normal file
6
arcfw/baselibc/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
Baselibc
|
||||
========
|
||||
This is a very simple libc for embedded systems. Mainly geared for 32-bit microcontrollers in the 10-100kB memory range.
|
||||
The library compiles to less than 5kB total on Cortex-M3, and much less if some functions aren't used.
|
||||
|
||||
The code is based on klibc and tinyprintf modules, and licensed under the BSD license.
|
||||
26
arcfw/baselibc/include/assert.h
Normal file
26
arcfw/baselibc/include/assert.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* assert.h
|
||||
*/
|
||||
|
||||
#ifndef _ASSERT_H
|
||||
#define _ASSERT_H
|
||||
|
||||
#ifdef NDEBUG
|
||||
|
||||
/*
|
||||
* NDEBUG doesn't just suppress the faulting behavior of assert(),
|
||||
* but also all side effects of the assert() argument. This behavior
|
||||
* is required by the C standard, and allows the argument to reference
|
||||
* variables that are not defined without NDEBUG.
|
||||
*/
|
||||
#define assert(x) ((void)(0))
|
||||
|
||||
#else
|
||||
|
||||
extern void __assert_fail(const char *, const char *, unsigned int);
|
||||
|
||||
#define assert(x) ((x) ? (void)0 : __assert_fail(#x, __FILE__, __LINE__))
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* _ASSERT_H */
|
||||
73
arcfw/baselibc/include/ctype.h
Normal file
73
arcfw/baselibc/include/ctype.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* ctype.h
|
||||
*
|
||||
* This assumes ASCII.
|
||||
*/
|
||||
|
||||
#ifndef _CTYPE_H
|
||||
#define _CTYPE_H
|
||||
|
||||
#include <klibc/extern.h>
|
||||
#include <klibc/inline.h>
|
||||
|
||||
__extern_inline int isupper(int __c)
|
||||
{
|
||||
return __c >= 'A' && __c <= 'Z';
|
||||
}
|
||||
|
||||
__extern_inline int islower(int __c)
|
||||
{
|
||||
return __c >= 'a' && __c <= 'z';
|
||||
}
|
||||
|
||||
__extern_inline int isalpha(int __c)
|
||||
{
|
||||
return islower(__c) || isupper(__c);
|
||||
}
|
||||
|
||||
__extern_inline int isdigit(int __c)
|
||||
{
|
||||
return ((unsigned)__c - '0') <= 9;
|
||||
}
|
||||
|
||||
__extern_inline int isalnum(int __c)
|
||||
{
|
||||
return isalpha(__c) || isdigit(__c);
|
||||
}
|
||||
|
||||
__extern_inline int isascii(int __c)
|
||||
{
|
||||
return !(__c & ~0x7f);
|
||||
}
|
||||
|
||||
__extern_inline int isblank(int __c)
|
||||
{
|
||||
return (__c == '\t') || (__c == ' ');
|
||||
}
|
||||
|
||||
__extern_inline int iscntrl(int __c)
|
||||
{
|
||||
return __c < 0x20;
|
||||
}
|
||||
|
||||
__extern_inline int isspace(int __c)
|
||||
{
|
||||
return __c == ' ' || __c == '\n' || __c == '\t' || __c == '\r';
|
||||
}
|
||||
|
||||
__extern_inline int isxdigit(int __c)
|
||||
{
|
||||
return isdigit(__c) || (__c >= 'a' && __c <= 'f') || (__c >= 'A' && __c <= 'F');
|
||||
}
|
||||
|
||||
__extern_inline int toupper(int __c)
|
||||
{
|
||||
return islower(__c) ? (__c & ~32) : __c;
|
||||
}
|
||||
|
||||
__extern_inline int tolower(int __c)
|
||||
{
|
||||
return isupper(__c) ? (__c | 32) : __c;
|
||||
}
|
||||
|
||||
#endif /* _CTYPE_H */
|
||||
13
arcfw/baselibc/include/dirent.h
Normal file
13
arcfw/baselibc/include/dirent.h
Normal file
@@ -0,0 +1,13 @@
|
||||
struct dirent {
|
||||
unsigned char _unknown_00[0xe];
|
||||
unsigned char d_type;
|
||||
unsigned char _unknown_0f;
|
||||
char d_name[];
|
||||
};
|
||||
#define DT_DIR 0x10
|
||||
|
||||
typedef void DIR;
|
||||
|
||||
DIR *opendir(const char *name);
|
||||
struct dirent *readdir(DIR *);
|
||||
int closedir(DIR *);
|
||||
2
arcfw/baselibc/include/errno.h
Normal file
2
arcfw/baselibc/include/errno.h
Normal file
@@ -0,0 +1,2 @@
|
||||
int *__errno(void);
|
||||
#define errno (*__errno())
|
||||
6
arcfw/baselibc/include/fcntl.h
Normal file
6
arcfw/baselibc/include/fcntl.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#define O_RDONLY 0x0000
|
||||
#define O_WRONLY 0x0001
|
||||
#define O_CREAT 0x0200
|
||||
#define O_TRUNC 0x0400
|
||||
|
||||
int open(const char *pathname, int flags, ...);
|
||||
226
arcfw/baselibc/include/inttypes.h
Normal file
226
arcfw/baselibc/include/inttypes.h
Normal file
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* inttypes.h
|
||||
*/
|
||||
|
||||
#ifndef _INTTYPES_H
|
||||
#define _INTTYPES_H
|
||||
|
||||
#include <klibc/extern.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static __inline__ intmax_t imaxabs(intmax_t __n)
|
||||
{
|
||||
return (__n < (intmax_t) 0) ? -__n : __n;
|
||||
}
|
||||
|
||||
__extern intmax_t strtoimax(const char *, char **, int);
|
||||
__extern uintmax_t strtoumax(const char *, char **, int);
|
||||
|
||||
/* extensions */
|
||||
__extern intmax_t strntoimax(const char *, char **, int, size_t);
|
||||
__extern uintmax_t strntoumax(const char *, char **, int, size_t);
|
||||
|
||||
#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS)
|
||||
|
||||
#define PRId8 "d"
|
||||
#define PRId16 "d"
|
||||
#define PRId32 "d"
|
||||
#define PRId64 __PRI64_RANK "d"
|
||||
|
||||
#define PRIdLEAST8 "d"
|
||||
#define PRIdLEAST16 "d"
|
||||
#define PRIdLEAST32 "d"
|
||||
#define PRIdLEAST64 __PRI64_RANK "d"
|
||||
|
||||
#define PRIdFAST8 "d"
|
||||
#define PRIdFAST16 __PRIFAST_RANK "d"
|
||||
#define PRIdFAST32 __PRIFAST_RANK "d"
|
||||
#define PRIdFAST64 __PRI64_RANK "d"
|
||||
|
||||
#define PRIdMAX __PRI64_RANK "d"
|
||||
#define PRIdPTR __PRIPTR_RANK "d"
|
||||
|
||||
#define PRIi8 "i"
|
||||
#define PRIi16 "i"
|
||||
#define PRIi32 "i"
|
||||
#define PRIi64 __PRI64_RANK "i"
|
||||
|
||||
#define PRIiLEAST8 "i"
|
||||
#define PRIiLEAST16 "i"
|
||||
#define PRIiLEAST32 "i"
|
||||
#define PRIiLEAST64 __PRI64_RANK "i"
|
||||
|
||||
#define PRIiFAST8 "i"
|
||||
#define PRIiFAST16 __PRIFAST_RANK "i"
|
||||
#define PRIiFAST32 __PRIFAST_RANK "i"
|
||||
#define PRIiFAST64 __PRI64_RANK "i"
|
||||
|
||||
#define PRIiMAX __PRI64_RANK "i"
|
||||
#define PRIiPTR __PRIPTR_RANK "i"
|
||||
|
||||
#define PRIo8 "o"
|
||||
#define PRIo16 "o"
|
||||
#define PRIo32 "o"
|
||||
#define PRIo64 __PRI64_RANK "o"
|
||||
|
||||
#define PRIoLEAST8 "o"
|
||||
#define PRIoLEAST16 "o"
|
||||
#define PRIoLEAST32 "o"
|
||||
#define PRIoLEAST64 __PRI64_RANK "o"
|
||||
|
||||
#define PRIoFAST8 "o"
|
||||
#define PRIoFAST16 __PRIFAST_RANK "o"
|
||||
#define PRIoFAST32 __PRIFAST_RANK "o"
|
||||
#define PRIoFAST64 __PRI64_RANK "o"
|
||||
|
||||
#define PRIoMAX __PRI64_RANK "o"
|
||||
#define PRIoPTR __PRIPTR_RANK "o"
|
||||
|
||||
#define PRIu8 "u"
|
||||
#define PRIu16 "u"
|
||||
#define PRIu32 "u"
|
||||
#define PRIu64 __PRI64_RANK "u"
|
||||
|
||||
#define PRIuLEAST8 "u"
|
||||
#define PRIuLEAST16 "u"
|
||||
#define PRIuLEAST32 "u"
|
||||
#define PRIuLEAST64 __PRI64_RANK "u"
|
||||
|
||||
#define PRIuFAST8 "u"
|
||||
#define PRIuFAST16 __PRIFAST_RANK "u"
|
||||
#define PRIuFAST32 __PRIFAST_RANK "u"
|
||||
#define PRIuFAST64 __PRI64_RANK "u"
|
||||
|
||||
#define PRIuMAX __PRI64_RANK "u"
|
||||
#define PRIuPTR __PRIPTR_RANK "u"
|
||||
|
||||
#define PRIx8 "x"
|
||||
#define PRIx16 "x"
|
||||
#define PRIx32 "x"
|
||||
#define PRIx64 __PRI64_RANK "x"
|
||||
|
||||
#define PRIxLEAST8 "x"
|
||||
#define PRIxLEAST16 "x"
|
||||
#define PRIxLEAST32 "x"
|
||||
#define PRIxLEAST64 __PRI64_RANK "x"
|
||||
|
||||
#define PRIxFAST8 "x"
|
||||
#define PRIxFAST16 __PRIFAST_RANK "x"
|
||||
#define PRIxFAST32 __PRIFAST_RANK "x"
|
||||
#define PRIxFAST64 __PRI64_RANK "x"
|
||||
|
||||
#define PRIxMAX __PRI64_RANK "x"
|
||||
#define PRIxPTR __PRIPTR_RANK "x"
|
||||
|
||||
#define PRIX8 "X"
|
||||
#define PRIX16 "X"
|
||||
#define PRIX32 "X"
|
||||
#define PRIX64 __PRI64_RANK "X"
|
||||
|
||||
#define PRIXLEAST8 "X"
|
||||
#define PRIXLEAST16 "X"
|
||||
#define PRIXLEAST32 "X"
|
||||
#define PRIXLEAST64 __PRI64_RANK "X"
|
||||
|
||||
#define PRIXFAST8 "X"
|
||||
#define PRIXFAST16 __PRIFAST_RANK "X"
|
||||
#define PRIXFAST32 __PRIFAST_RANK "X"
|
||||
#define PRIXFAST64 __PRI64_RANK "X"
|
||||
|
||||
#define PRIXMAX __PRI64_RANK "X"
|
||||
#define PRIXPTR __PRIPTR_RANK "X"
|
||||
|
||||
#define SCNd8 "hhd"
|
||||
#define SCNd16 "hd"
|
||||
#define SCNd32 "d"
|
||||
#define SCNd64 __PRI64_RANK "d"
|
||||
|
||||
#define SCNdLEAST8 "hhd"
|
||||
#define SCNdLEAST16 "hd"
|
||||
#define SCNdLEAST32 "d"
|
||||
#define SCNdLEAST64 __PRI64_RANK "d"
|
||||
|
||||
#define SCNdFAST8 "hhd"
|
||||
#define SCNdFAST16 __PRIFAST_RANK "d"
|
||||
#define SCNdFAST32 __PRIFAST_RANK "d"
|
||||
#define SCNdFAST64 __PRI64_RANK "d"
|
||||
|
||||
#define SCNdMAX __PRI64_RANK "d"
|
||||
#define SCNdPTR __PRIPTR_RANK "d"
|
||||
|
||||
#define SCNi8 "hhi"
|
||||
#define SCNi16 "hi"
|
||||
#define SCNi32 "i"
|
||||
#define SCNi64 __PRI64_RANK "i"
|
||||
|
||||
#define SCNiLEAST8 "hhi"
|
||||
#define SCNiLEAST16 "hi"
|
||||
#define SCNiLEAST32 "i"
|
||||
#define SCNiLEAST64 __PRI64_RANK "i"
|
||||
|
||||
#define SCNiFAST8 "hhi"
|
||||
#define SCNiFAST16 __PRIFAST_RANK "i"
|
||||
#define SCNiFAST32 __PRIFAST_RANK "i"
|
||||
#define SCNiFAST64 __PRI64_RANK "i"
|
||||
|
||||
#define SCNiMAX __PRI64_RANK "i"
|
||||
#define SCNiPTR __PRIPTR_RANK "i"
|
||||
|
||||
#define SCNo8 "hho"
|
||||
#define SCNo16 "ho"
|
||||
#define SCNo32 "o"
|
||||
#define SCNo64 __PRI64_RANK "o"
|
||||
|
||||
#define SCNoLEAST8 "hho"
|
||||
#define SCNoLEAST16 "ho"
|
||||
#define SCNoLEAST32 "o"
|
||||
#define SCNoLEAST64 __PRI64_RANK "o"
|
||||
|
||||
#define SCNoFAST8 "hho"
|
||||
#define SCNoFAST16 __PRIFAST_RANK "o"
|
||||
#define SCNoFAST32 __PRIFAST_RANK "o"
|
||||
#define SCNoFAST64 __PRI64_RANK "o"
|
||||
|
||||
#define SCNoMAX __PRI64_RANK "o"
|
||||
#define SCNoPTR __PRIPTR_RANK "o"
|
||||
|
||||
#define SCNu8 "hhu"
|
||||
#define SCNu16 "hu"
|
||||
#define SCNu32 "u"
|
||||
#define SCNu64 __PRI64_RANK "u"
|
||||
|
||||
#define SCNuLEAST8 "hhu"
|
||||
#define SCNuLEAST16 "hu"
|
||||
#define SCNuLEAST32 "u"
|
||||
#define SCNuLEAST64 __PRI64_RANK "u"
|
||||
|
||||
#define SCNuFAST8 "hhu"
|
||||
#define SCNuFAST16 __PRIFAST_RANK "u"
|
||||
#define SCNuFAST32 __PRIFAST_RANK "u"
|
||||
#define SCNuFAST64 __PRI64_RANK "u"
|
||||
|
||||
#define SCNuMAX __PRI64_RANK "u"
|
||||
#define SCNuPTR __PRIPTR_RANK "u"
|
||||
|
||||
#define SCNx8 "hhx"
|
||||
#define SCNx16 "hx"
|
||||
#define SCNx32 "x"
|
||||
#define SCNx64 __PRI64_RANK "x"
|
||||
|
||||
#define SCNxLEAST8 "hhx"
|
||||
#define SCNxLEAST16 "hx"
|
||||
#define SCNxLEAST32 "x"
|
||||
#define SCNxLEAST64 __PRI64_RANK "x"
|
||||
|
||||
#define SCNxFAST8 "hhx"
|
||||
#define SCNxFAST16 __PRIFAST_RANK "x"
|
||||
#define SCNxFAST32 __PRIFAST_RANK "x"
|
||||
#define SCNxFAST64 __PRI64_RANK "x"
|
||||
|
||||
#define SCNxMAX __PRI64_RANK "x"
|
||||
#define SCNxPTR __PRIPTR_RANK "x"
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* _INTTYPES_H */
|
||||
16
arcfw/baselibc/include/klibc/extern.h
Normal file
16
arcfw/baselibc/include/klibc/extern.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* klibc/extern.h
|
||||
*/
|
||||
|
||||
#ifndef _KLIBC_EXTERN_H
|
||||
#define _KLIBC_EXTERN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define __extern extern "C"
|
||||
#else
|
||||
#define __extern extern
|
||||
#endif
|
||||
|
||||
#define __alias(x) __attribute__((weak, alias(x)))
|
||||
|
||||
#endif /* _KLIBC_EXTERN_H */
|
||||
12
arcfw/baselibc/include/klibc/inline.h
Normal file
12
arcfw/baselibc/include/klibc/inline.h
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* klibc/inline.h
|
||||
*/
|
||||
|
||||
#ifndef _KLIBC_INLINE_H
|
||||
#define _KLIBC_INLINE_H
|
||||
|
||||
#ifndef __extern_inline
|
||||
#define __extern_inline extern inline __attribute__((gnu_inline))
|
||||
#endif
|
||||
|
||||
#endif /* _KLIBC_INLINE_H */
|
||||
7
arcfw/baselibc/include/pthread.h
Normal file
7
arcfw/baselibc/include/pthread.h
Normal file
@@ -0,0 +1,7 @@
|
||||
typedef void* pthread_t;
|
||||
typedef unsigned char pthread_attr_t[0x28];
|
||||
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
||||
void *(*start_routine) (void *), void *arg);
|
||||
int pthread_join(pthread_t thread, void **retval);
|
||||
int pthread_cancel(pthread_t thread);
|
||||
void pthread_exit(void *retval);
|
||||
140
arcfw/baselibc/include/stdio.h
Normal file
140
arcfw/baselibc/include/stdio.h
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* stdio.h
|
||||
*/
|
||||
|
||||
#ifndef _STDIO_H
|
||||
#define _STDIO_H
|
||||
|
||||
#include <klibc/extern.h>
|
||||
#include <klibc/inline.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
/* The File structure is designed to be compatible with ChibiOS/RT type
|
||||
* BaseSequentialStream.
|
||||
*/
|
||||
struct File;
|
||||
|
||||
typedef struct File FILE;
|
||||
#else
|
||||
typedef void FILE;
|
||||
#endif
|
||||
|
||||
#ifdef BASELIBC_INTERNAL
|
||||
struct File_methods
|
||||
{
|
||||
size_t (*write)(FILE* instance, const char *bp, size_t n);
|
||||
size_t (*read)(FILE* instance, char *bp, size_t n);
|
||||
};
|
||||
|
||||
struct File
|
||||
{
|
||||
const struct File_methods *vmt;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef EOF
|
||||
# define EOF (-1)
|
||||
#endif
|
||||
|
||||
/* Standard file descriptors - implement these globals yourself. */
|
||||
extern FILE* stdin;
|
||||
extern FILE* stdout;
|
||||
extern FILE* stderr;
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
/* Wrappers around stream write and read */
|
||||
__extern_inline size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
||||
{
|
||||
if (stream->vmt->read == NULL) return 0;
|
||||
return stream->vmt->read(stream, buf, size*nmemb) / size;
|
||||
}
|
||||
|
||||
__extern_inline size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
||||
{
|
||||
if (stream->vmt->write == NULL) return 0;
|
||||
return stream->vmt->write(stream, buf, size*nmemb) / size;
|
||||
}
|
||||
#else
|
||||
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream);
|
||||
size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream);
|
||||
#endif
|
||||
|
||||
__extern_inline int fputs(const char *s, FILE *f)
|
||||
{
|
||||
return fwrite(s, 1, strlen(s), f);
|
||||
}
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
#if 0
|
||||
__extern_inline int puts(const char *s)
|
||||
{
|
||||
return fwrite(s, 1, strlen(s), stdout) + fwrite("\n", 1, 1, stdout);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
__extern_inline int fputc(int c, FILE *f)
|
||||
{
|
||||
unsigned char ch = c;
|
||||
return fwrite(&ch, 1, 1, f) == 1 ? ch : EOF;
|
||||
}
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
__extern char *fgets(char *, int, FILE *);
|
||||
__extern_inline int fgetc(FILE *f)
|
||||
{
|
||||
unsigned char ch;
|
||||
return fread(&ch, 1, 1, f) == 1 ? ch : EOF;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define putc(c,f) fputc((c),(f))
|
||||
#define putchar(c) fputc((c),stdout)
|
||||
#define getc(f) fgetc(f)
|
||||
#define getchar() fgetc(stdin)
|
||||
|
||||
__extern int printf(const char *, ...);
|
||||
__extern int vprintf(const char *, va_list);
|
||||
__extern int fprintf(FILE *, const char *, ...);
|
||||
__extern int vfprintf(FILE *, const char *, va_list);
|
||||
__extern int sprintf(char *, const char *, ...);
|
||||
__extern int vsprintf(char *, const char *, va_list);
|
||||
__extern int snprintf(char *, size_t n, const char *, ...);
|
||||
__extern int vsnprintf(char *, size_t n, const char *, va_list);
|
||||
__extern int asprintf(char **, const char *, ...);
|
||||
__extern int vasprintf(char **, const char *, va_list);
|
||||
|
||||
__extern int sscanf(const char *, const char *, ...);
|
||||
__extern int vsscanf(const char *, const char *, va_list);
|
||||
|
||||
#if defined(WITH_STDIO) || defined(BASELIBC_INTERNAL)
|
||||
/* Open a memory buffer for writing.
|
||||
Note: Does not write null terminator.*/
|
||||
struct MemFile
|
||||
{
|
||||
struct File file;
|
||||
char *buffer;
|
||||
size_t bytes_written;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
FILE *fmemopen_w(struct MemFile* storage, char *buffer, size_t size);
|
||||
#endif
|
||||
|
||||
int remove(const char *pathname);
|
||||
int rename(const char *oldpath, const char *newpath);
|
||||
|
||||
FILE *fopen(const char *path, const char *mode);
|
||||
int fclose(FILE *);
|
||||
int fseek(FILE *, long offset, int whence);
|
||||
long ftell(FILE *);
|
||||
void setbuf(FILE *stream, char *buf);
|
||||
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
char *fgets(char *s, int size, FILE *stream);
|
||||
|
||||
#endif /* _STDIO_H */
|
||||
86
arcfw/baselibc/include/stdlib.h
Normal file
86
arcfw/baselibc/include/stdlib.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* stdlib.h
|
||||
*/
|
||||
|
||||
#ifndef _STDLIB_H
|
||||
#define _STDLIB_H
|
||||
|
||||
#include <klibc/extern.h>
|
||||
#include <klibc/inline.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
__extern_inline int abs(int __n)
|
||||
{
|
||||
return (__n < 0) ? -__n : __n;
|
||||
}
|
||||
|
||||
__extern int atoi(const char *);
|
||||
__extern long atol(const char *);
|
||||
__extern long long atoll(const char *);
|
||||
|
||||
__extern_inline long labs(long __n)
|
||||
{
|
||||
return (__n < 0L) ? -__n : __n;
|
||||
}
|
||||
|
||||
__extern_inline long long llabs(long long __n)
|
||||
{
|
||||
return (__n < 0LL) ? -__n : __n;
|
||||
}
|
||||
|
||||
__extern void free(void *);
|
||||
__extern void *malloc(size_t);
|
||||
__extern void *calloc(size_t, size_t);
|
||||
__extern void *realloc(void *, size_t);
|
||||
|
||||
/* Giving malloc some memory from which to allocate */
|
||||
__extern void add_malloc_block(void *, size_t);
|
||||
__extern void get_malloc_memory_status(size_t *, size_t *);
|
||||
|
||||
/* Malloc locking
|
||||
* Until the callbacks are set, malloc doesn't do any locking.
|
||||
* malloc_lock() *may* timeout, in which case malloc() will return NULL.
|
||||
*/
|
||||
typedef bool (*malloc_lock_t)();
|
||||
typedef void (*malloc_unlock_t)();
|
||||
__extern void set_malloc_locking(malloc_lock_t, malloc_unlock_t);
|
||||
|
||||
__extern long strtol(const char *, char **, int);
|
||||
__extern long long strtoll(const char *, char **, int);
|
||||
__extern unsigned long strtoul(const char *, char **, int);
|
||||
__extern unsigned long long strtoull(const char *, char **, int);
|
||||
|
||||
typedef int (*__comparefunc_t) (const void *, const void *);
|
||||
__extern void *bsearch(const void *, const void *, size_t, size_t,
|
||||
__comparefunc_t);
|
||||
__extern void qsort(void *, size_t, size_t, __comparefunc_t);
|
||||
|
||||
__extern long jrand48(unsigned short *);
|
||||
__extern long mrand48(void);
|
||||
__extern long nrand48(unsigned short *);
|
||||
__extern long lrand48(void);
|
||||
__extern unsigned short *seed48(const unsigned short *);
|
||||
__extern void srand48(long);
|
||||
|
||||
#define RAND_MAX 0x7fffffff
|
||||
__extern_inline int rand(void)
|
||||
{
|
||||
return (int)lrand48();
|
||||
}
|
||||
__extern_inline void srand(unsigned int __s)
|
||||
{
|
||||
srand48(__s);
|
||||
}
|
||||
__extern_inline long random(void)
|
||||
{
|
||||
return lrand48();
|
||||
}
|
||||
__extern_inline void srandom(unsigned int __s)
|
||||
{
|
||||
srand48(__s);
|
||||
}
|
||||
|
||||
__extern void abort(void);
|
||||
|
||||
#endif /* _STDLIB_H */
|
||||
59
arcfw/baselibc/include/string.h
Normal file
59
arcfw/baselibc/include/string.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* string.h
|
||||
*/
|
||||
|
||||
#ifndef _STRING_H
|
||||
#define _STRING_H
|
||||
|
||||
#include <klibc/extern.h>
|
||||
#include <stddef.h>
|
||||
|
||||
__extern void *memccpy(void *, const void *, int, size_t);
|
||||
__extern void *memchr(const void *, int, size_t);
|
||||
__extern void *memrchr(const void *, int, size_t);
|
||||
__extern int memcmp(const void *, const void *, size_t);
|
||||
__extern void *memcpy(void *, const void *, size_t);
|
||||
__extern void *memmove(void *, const void *, size_t);
|
||||
__extern void *memset(void *, int, size_t);
|
||||
__extern void *memmem(const void *, size_t, const void *, size_t);
|
||||
__extern void memswap(void *, void *, size_t);
|
||||
__extern void bzero(void *, size_t);
|
||||
__extern int strcasecmp(const char *, const char *);
|
||||
__extern int strncasecmp(const char *, const char *, size_t);
|
||||
__extern char *strcat(char *, const char *);
|
||||
__extern char *strchr(const char *, int);
|
||||
__extern char *index(const char *, int);
|
||||
__extern char *strrchr(const char *, int);
|
||||
__extern char *rindex(const char *, int);
|
||||
__extern int strcmp(const char *, const char *);
|
||||
__extern char *strcpy(char *, const char *);
|
||||
__extern size_t strcspn(const char *, const char *);
|
||||
__extern char *strdup(const char *);
|
||||
__extern char *strndup(const char *, size_t);
|
||||
__extern size_t strlen(const char *);
|
||||
__extern size_t strnlen(const char *, size_t);
|
||||
__extern char *strncat(char *, const char *, size_t);
|
||||
__extern size_t strlcat(char *, const char *, size_t);
|
||||
__extern int strncmp(const char *, const char *, size_t);
|
||||
__extern char *strncpy(char *, const char *, size_t);
|
||||
__extern size_t strlcpy(char *, const char *, size_t);
|
||||
__extern char *strpbrk(const char *, const char *);
|
||||
__extern char *strsep(char **, const char *);
|
||||
__extern size_t strspn(const char *, const char *);
|
||||
__extern char *strstr(const char *, const char *);
|
||||
__extern char *strtok(char *, const char *);
|
||||
__extern char *strtok_r(char *, const char *, char **);
|
||||
|
||||
/* Some dummy functions to avoid errors with C++ cstring */
|
||||
inline static int strcoll(const char *s1, const char *s2)
|
||||
{
|
||||
return strcmp(s1, s2);
|
||||
}
|
||||
|
||||
inline static size_t strxfrm(char *dest, const char *src, size_t n)
|
||||
{
|
||||
strncpy(dest, src, n);
|
||||
return strlen(src);
|
||||
}
|
||||
|
||||
#endif /* _STRING_H */
|
||||
1
arcfw/baselibc/include/sys/stat.h
Normal file
1
arcfw/baselibc/include/sys/stat.h
Normal file
@@ -0,0 +1 @@
|
||||
int mkdir(const char *pathname, unsigned int mode);
|
||||
6
arcfw/baselibc/include/sys/time.h
Normal file
6
arcfw/baselibc/include/sys/time.h
Normal file
@@ -0,0 +1,6 @@
|
||||
struct timeval {
|
||||
unsigned int tv_sec;
|
||||
unsigned int tv_usec;
|
||||
};
|
||||
|
||||
int gettimeofday(struct timeval *tv, void *tz);
|
||||
3
arcfw/baselibc/include/sys/types.h
Normal file
3
arcfw/baselibc/include/sys/types.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#define SEEK_SET 0
|
||||
#define SEEK_CUR 1
|
||||
#define SEEK_END 2
|
||||
6
arcfw/baselibc/include/unistd.h
Normal file
6
arcfw/baselibc/include/unistd.h
Normal file
@@ -0,0 +1,6 @@
|
||||
int write(int fd, const void *buf, unsigned int count);
|
||||
int read(int fd, void *buf, unsigned int count);
|
||||
int close(int fd);
|
||||
int stat(const char *pathname, void *buf);
|
||||
|
||||
int usleep(int usecs);
|
||||
30
arcfw/baselibc/src/asprintf.c
Normal file
30
arcfw/baselibc/src/asprintf.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* asprintf.c
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int asprintf(char **bufp, const char *format, ...)
|
||||
{
|
||||
va_list ap, ap1;
|
||||
int rv;
|
||||
int bytes;
|
||||
char *p;
|
||||
|
||||
va_start(ap, format);
|
||||
va_copy(ap1, ap);
|
||||
|
||||
bytes = vsnprintf(NULL, 0, format, ap1) + 1;
|
||||
va_end(ap1);
|
||||
|
||||
*bufp = p = malloc(bytes);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
rv = vsnprintf(p, bytes, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
return rv;
|
||||
}
|
||||
3
arcfw/baselibc/src/atoi.c
Normal file
3
arcfw/baselibc/src/atoi.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE int
|
||||
#define NAME atoi
|
||||
#include "atox.c"
|
||||
3
arcfw/baselibc/src/atol.c
Normal file
3
arcfw/baselibc/src/atol.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE long
|
||||
#define NAME atol
|
||||
#include "atox.c"
|
||||
3
arcfw/baselibc/src/atoll.c
Normal file
3
arcfw/baselibc/src/atoll.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE long long
|
||||
#define NAME atoll
|
||||
#include "atox.c"
|
||||
26
arcfw/baselibc/src/bsearch.c
Normal file
26
arcfw/baselibc/src/bsearch.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* bsearch.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void *bsearch(const void *key, const void *base, size_t nmemb,
|
||||
size_t size, int (*cmp) (const void *, const void *))
|
||||
{
|
||||
while (nmemb) {
|
||||
size_t mididx = nmemb / 2;
|
||||
const void *midobj = base + mididx * size;
|
||||
int diff = cmp(key, midobj);
|
||||
|
||||
if (diff == 0)
|
||||
return (void *)midobj;
|
||||
|
||||
if (diff > 0) {
|
||||
base = midobj + size;
|
||||
nmemb -= mididx + 1;
|
||||
} else
|
||||
nmemb = mididx;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
6
arcfw/baselibc/src/bzero.c
Normal file
6
arcfw/baselibc/src/bzero.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <string.h>
|
||||
|
||||
void bzero(void *dst, size_t n)
|
||||
{
|
||||
memset(dst, 0, n);
|
||||
}
|
||||
20
arcfw/baselibc/src/calloc.c
Normal file
20
arcfw/baselibc/src/calloc.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* calloc.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* FIXME: This should look for multiplication overflow */
|
||||
|
||||
void *calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
size *= nmemb;
|
||||
ptr = malloc(size);
|
||||
if (ptr)
|
||||
memset(ptr, 0, size);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
33
arcfw/baselibc/src/fgets.c
Normal file
33
arcfw/baselibc/src/fgets.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* fgets.c
|
||||
*
|
||||
* This will be very slow due to the implementation of getc(),
|
||||
* but we don't have anywhere to put characters we don't need from
|
||||
* the input.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
char *fgets(char *s, int n, FILE *f)
|
||||
{
|
||||
int ch;
|
||||
char *p = s;
|
||||
|
||||
while (n > 1) {
|
||||
ch = getc(f);
|
||||
if (ch == EOF) {
|
||||
*p = '\0';
|
||||
return NULL;
|
||||
}
|
||||
*p++ = ch;
|
||||
n--;
|
||||
if (ch == '\n')
|
||||
break;
|
||||
}
|
||||
if (n)
|
||||
*p = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
8
arcfw/baselibc/src/gettimeofday.c
Normal file
8
arcfw/baselibc/src/gettimeofday.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <sys/time.h>
|
||||
|
||||
int do_gettimeofday(struct timeval *tv);
|
||||
int gettimeofday(struct timeval *tv, void *tz)
|
||||
{
|
||||
(void)tz;
|
||||
return do_gettimeofday(tv);
|
||||
}
|
||||
5
arcfw/baselibc/src/inline.c
Normal file
5
arcfw/baselibc/src/inline.c
Normal file
@@ -0,0 +1,5 @@
|
||||
// Make an externally visible symbol out of inlined functions
|
||||
#define __extern_inline
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
24
arcfw/baselibc/src/jrand48.c
Normal file
24
arcfw/baselibc/src/jrand48.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* jrand48.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
long jrand48(unsigned short* xsubi)
|
||||
{
|
||||
uint64_t x;
|
||||
|
||||
/* The xsubi[] array is littleendian by spec */
|
||||
x = (uint64_t) (uint16_t) xsubi[0] +
|
||||
((uint64_t) (uint16_t) xsubi[1] << 16) +
|
||||
((uint64_t) (uint16_t) xsubi[2] << 32);
|
||||
|
||||
x = (0x5deece66dULL * x) + 0xb;
|
||||
|
||||
xsubi[0] = (unsigned short)(uint16_t) x;
|
||||
xsubi[1] = (unsigned short)(uint16_t) (x >> 16);
|
||||
xsubi[2] = (unsigned short)(uint16_t) (x >> 32);
|
||||
|
||||
return (long)(int32_t) (x >> 16);
|
||||
}
|
||||
13
arcfw/baselibc/src/lrand48.c
Normal file
13
arcfw/baselibc/src/lrand48.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* lrand48.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern unsigned short __rand48_seed[3]; /* Common with mrand48.c, srand48.c */
|
||||
|
||||
long lrand48(void)
|
||||
{
|
||||
return (uint32_t) jrand48(__rand48_seed) >> 1;
|
||||
}
|
||||
267
arcfw/baselibc/src/malloc.c
Normal file
267
arcfw/baselibc/src/malloc.c
Normal file
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* malloc.c
|
||||
*
|
||||
* Very simple linked-list based malloc()/free().
|
||||
*/
|
||||
|
||||
#ifdef WITH_MALLOC
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "malloc.h"
|
||||
|
||||
/* Both the arena list and the free memory list are double linked
|
||||
list with head node. This the head node. Note that the arena list
|
||||
is sorted in order of address. */
|
||||
static struct free_arena_header __malloc_head = {
|
||||
{
|
||||
ARENA_TYPE_HEAD,
|
||||
0,
|
||||
&__malloc_head,
|
||||
&__malloc_head,
|
||||
},
|
||||
&__malloc_head,
|
||||
&__malloc_head
|
||||
};
|
||||
|
||||
static bool malloc_lock_nop() {return true;}
|
||||
static void malloc_unlock_nop() {}
|
||||
|
||||
static malloc_lock_t malloc_lock = &malloc_lock_nop;
|
||||
static malloc_unlock_t malloc_unlock = &malloc_unlock_nop;
|
||||
|
||||
static inline void mark_block_dead(struct free_arena_header *ah)
|
||||
{
|
||||
#ifdef DEBUG_MALLOC
|
||||
ah->a.type = ARENA_TYPE_DEAD;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void remove_from_main_chain(struct free_arena_header *ah)
|
||||
{
|
||||
struct free_arena_header *ap, *an;
|
||||
|
||||
mark_block_dead(ah);
|
||||
|
||||
ap = ah->a.prev;
|
||||
an = ah->a.next;
|
||||
ap->a.next = an;
|
||||
an->a.prev = ap;
|
||||
}
|
||||
|
||||
static inline void remove_from_free_chain(struct free_arena_header *ah)
|
||||
{
|
||||
struct free_arena_header *ap, *an;
|
||||
|
||||
ap = ah->prev_free;
|
||||
an = ah->next_free;
|
||||
ap->next_free = an;
|
||||
an->prev_free = ap;
|
||||
}
|
||||
|
||||
static inline void remove_from_chains(struct free_arena_header *ah)
|
||||
{
|
||||
remove_from_free_chain(ah);
|
||||
remove_from_main_chain(ah);
|
||||
}
|
||||
|
||||
static void *__malloc_from_block(struct free_arena_header *fp, size_t size)
|
||||
{
|
||||
size_t fsize;
|
||||
struct free_arena_header *nfp, *na, *fpn, *fpp;
|
||||
|
||||
fsize = fp->a.size;
|
||||
|
||||
/* We need the 2* to account for the larger requirements of a
|
||||
free block */
|
||||
if (fsize >= size + 2 * sizeof(struct arena_header)) {
|
||||
/* Bigger block than required -- split block */
|
||||
nfp = (struct free_arena_header *)((char *)fp + size);
|
||||
na = fp->a.next;
|
||||
|
||||
nfp->a.type = ARENA_TYPE_FREE;
|
||||
nfp->a.size = fsize - size;
|
||||
fp->a.type = ARENA_TYPE_USED;
|
||||
fp->a.size = size;
|
||||
|
||||
/* Insert into all-block chain */
|
||||
nfp->a.prev = fp;
|
||||
nfp->a.next = na;
|
||||
na->a.prev = nfp;
|
||||
fp->a.next = nfp;
|
||||
|
||||
/* Replace current block on free chain */
|
||||
nfp->next_free = fpn = fp->next_free;
|
||||
nfp->prev_free = fpp = fp->prev_free;
|
||||
fpn->prev_free = nfp;
|
||||
fpp->next_free = nfp;
|
||||
} else {
|
||||
fp->a.type = ARENA_TYPE_USED; /* Allocate the whole block */
|
||||
remove_from_free_chain(fp);
|
||||
}
|
||||
|
||||
return (void *)(&fp->a + 1);
|
||||
}
|
||||
|
||||
static struct free_arena_header *__free_block(struct free_arena_header *ah)
|
||||
{
|
||||
struct free_arena_header *pah, *nah;
|
||||
|
||||
pah = ah->a.prev;
|
||||
nah = ah->a.next;
|
||||
if (pah->a.type == ARENA_TYPE_FREE &&
|
||||
(char *)pah + pah->a.size == (char *)ah) {
|
||||
/* Coalesce into the previous block */
|
||||
pah->a.size += ah->a.size;
|
||||
pah->a.next = nah;
|
||||
nah->a.prev = pah;
|
||||
mark_block_dead(ah);
|
||||
|
||||
ah = pah;
|
||||
pah = ah->a.prev;
|
||||
} else {
|
||||
/* Need to add this block to the free chain */
|
||||
ah->a.type = ARENA_TYPE_FREE;
|
||||
|
||||
ah->next_free = __malloc_head.next_free;
|
||||
ah->prev_free = &__malloc_head;
|
||||
__malloc_head.next_free = ah;
|
||||
ah->next_free->prev_free = ah;
|
||||
}
|
||||
|
||||
/* In either of the previous cases, we might be able to merge
|
||||
with the subsequent block... */
|
||||
if (nah->a.type == ARENA_TYPE_FREE &&
|
||||
(char *)ah + ah->a.size == (char *)nah) {
|
||||
ah->a.size += nah->a.size;
|
||||
|
||||
/* Remove the old block from the chains */
|
||||
remove_from_chains(nah);
|
||||
}
|
||||
|
||||
/* Return the block that contains the called block */
|
||||
return ah;
|
||||
}
|
||||
|
||||
void *malloc(size_t size)
|
||||
{
|
||||
struct free_arena_header *fp;
|
||||
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
/* Add the obligatory arena header, and round up */
|
||||
size = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
|
||||
|
||||
if (!malloc_lock())
|
||||
return NULL;
|
||||
|
||||
void *result = NULL;
|
||||
for (fp = __malloc_head.next_free; fp->a.type != ARENA_TYPE_HEAD;
|
||||
fp = fp->next_free) {
|
||||
if (fp->a.size >= size) {
|
||||
/* Found fit -- allocate out of this block */
|
||||
result = __malloc_from_block(fp, size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
malloc_unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Call this to give malloc some memory to allocate from */
|
||||
void add_malloc_block(void *buf, size_t size)
|
||||
{
|
||||
struct free_arena_header *fp = buf;
|
||||
struct free_arena_header *pah;
|
||||
|
||||
if (size < sizeof(struct free_arena_header))
|
||||
return; // Too small.
|
||||
|
||||
/* Insert the block into the management chains. We need to set
|
||||
up the size and the main block list pointer, the rest of
|
||||
the work is logically identical to free(). */
|
||||
fp->a.type = ARENA_TYPE_FREE;
|
||||
fp->a.size = size;
|
||||
|
||||
if (!malloc_lock())
|
||||
return;
|
||||
|
||||
/* We need to insert this into the main block list in the proper
|
||||
place -- this list is required to be sorted. Since we most likely
|
||||
get memory assignments in ascending order, search backwards for
|
||||
the proper place. */
|
||||
for (pah = __malloc_head.a.prev; pah->a.type != ARENA_TYPE_HEAD;
|
||||
pah = pah->a.prev) {
|
||||
if (pah < fp)
|
||||
break;
|
||||
}
|
||||
|
||||
/* Now pah points to the node that should be the predecessor of
|
||||
the new node */
|
||||
fp->a.next = pah->a.next;
|
||||
fp->a.prev = pah;
|
||||
pah->a.next = fp;
|
||||
fp->a.next->a.prev = fp;
|
||||
|
||||
/* Insert into the free chain and coalesce with adjacent blocks */
|
||||
fp = __free_block(fp);
|
||||
|
||||
malloc_unlock();
|
||||
}
|
||||
|
||||
void free(void *ptr)
|
||||
{
|
||||
struct free_arena_header *ah;
|
||||
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
ah = (struct free_arena_header *)
|
||||
((struct arena_header *)ptr - 1);
|
||||
|
||||
#ifdef DEBUG_MALLOC
|
||||
assert(ah->a.type == ARENA_TYPE_USED);
|
||||
#endif
|
||||
|
||||
if (!malloc_lock())
|
||||
return;
|
||||
|
||||
/* Merge into adjacent free blocks */
|
||||
ah = __free_block(ah);
|
||||
malloc_unlock();
|
||||
}
|
||||
|
||||
void get_malloc_memory_status(size_t *free_bytes, size_t *largest_block)
|
||||
{
|
||||
struct free_arena_header *fp;
|
||||
*free_bytes = 0;
|
||||
*largest_block = 0;
|
||||
|
||||
if (!malloc_lock())
|
||||
return;
|
||||
|
||||
for (fp = __malloc_head.next_free; fp->a.type != ARENA_TYPE_HEAD; fp = fp->next_free) {
|
||||
*free_bytes += fp->a.size;
|
||||
if (fp->a.size >= *largest_block) {
|
||||
*largest_block = fp->a.size;
|
||||
}
|
||||
}
|
||||
|
||||
malloc_unlock();
|
||||
}
|
||||
|
||||
void set_malloc_locking(malloc_lock_t lock, malloc_unlock_t unlock)
|
||||
{
|
||||
if (lock)
|
||||
malloc_lock = lock;
|
||||
else
|
||||
malloc_lock = &malloc_lock_nop;
|
||||
|
||||
if (unlock)
|
||||
malloc_unlock = unlock;
|
||||
else
|
||||
malloc_unlock = &malloc_unlock_nop;
|
||||
}
|
||||
#endif
|
||||
43
arcfw/baselibc/src/malloc.h
Normal file
43
arcfw/baselibc/src/malloc.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* malloc.h
|
||||
*
|
||||
* Internals for the memory allocator
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* This structure should be a power of two. This becomes the
|
||||
* alignment unit.
|
||||
*/
|
||||
struct free_arena_header;
|
||||
|
||||
struct arena_header {
|
||||
size_t type;
|
||||
size_t size;
|
||||
struct free_arena_header *next, *prev;
|
||||
};
|
||||
|
||||
#ifdef DEBUG_MALLOC
|
||||
#define ARENA_TYPE_USED 0x64e69c70
|
||||
#define ARENA_TYPE_FREE 0x012d610a
|
||||
#define ARENA_TYPE_HEAD 0x971676b5
|
||||
#define ARENA_TYPE_DEAD 0xeeeeeeee
|
||||
#else
|
||||
#define ARENA_TYPE_USED 0
|
||||
#define ARENA_TYPE_FREE 1
|
||||
#define ARENA_TYPE_HEAD 2
|
||||
#endif
|
||||
|
||||
#define ARENA_SIZE_MASK (~(sizeof(struct arena_header)-1))
|
||||
|
||||
/*
|
||||
* This structure should be no more than twice the size of the
|
||||
* previous structure.
|
||||
*/
|
||||
struct free_arena_header {
|
||||
struct arena_header a;
|
||||
struct free_arena_header *next_free, *prev_free;
|
||||
};
|
||||
|
||||
23
arcfw/baselibc/src/memccpy.c
Normal file
23
arcfw/baselibc/src/memccpy.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* memccpy.c
|
||||
*
|
||||
* memccpy()
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void *memccpy(void *dst, const void *src, int c, size_t n)
|
||||
{
|
||||
char *q = dst;
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
while (n--) {
|
||||
*q++ = ch = *p++;
|
||||
if (ch == (char)c)
|
||||
return q;
|
||||
}
|
||||
|
||||
return NULL; /* No instance of "c" found */
|
||||
}
|
||||
19
arcfw/baselibc/src/memchr.c
Normal file
19
arcfw/baselibc/src/memchr.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* memchr.c
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void *memchr(const void *s, int c, size_t n)
|
||||
{
|
||||
const unsigned char *sp = s;
|
||||
|
||||
while (n--) {
|
||||
if (*sp == (unsigned char)c)
|
||||
return (void *)sp;
|
||||
sp++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
19
arcfw/baselibc/src/memcmp.c
Normal file
19
arcfw/baselibc/src/memcmp.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* memcmp.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
const unsigned char *c1 = s1, *c2 = s2;
|
||||
int d = 0;
|
||||
|
||||
while (n--) {
|
||||
d = (int)*c1++ - (int)*c2++;
|
||||
if (d)
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
29
arcfw/baselibc/src/memcpy.c
Normal file
29
arcfw/baselibc/src/memcpy.c
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* memcpy.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *memcpy(void *dst, const void *src, size_t n)
|
||||
{
|
||||
const char *p = src;
|
||||
char *q = dst;
|
||||
#if defined(__i386__)
|
||||
size_t nl = n >> 2;
|
||||
asm volatile ("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb":"+c" (nl),
|
||||
"+S"(p), "+D"(q)
|
||||
:"r"(n & 3));
|
||||
#elif defined(__x86_64__)
|
||||
size_t nq = n >> 3;
|
||||
asm volatile ("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb":"+c"
|
||||
(nq), "+S"(p), "+D"(q)
|
||||
:"r"((uint32_t) (n & 7)));
|
||||
#else
|
||||
while (n--) {
|
||||
*q++ = *p++;
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
33
arcfw/baselibc/src/memfile.c
Normal file
33
arcfw/baselibc/src/memfile.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
size_t memfile_write(FILE *instance, const char *bp, size_t n)
|
||||
{
|
||||
struct MemFile *f = (struct MemFile*)instance;
|
||||
size_t i = 0;
|
||||
|
||||
while (n--)
|
||||
{
|
||||
f->bytes_written++;
|
||||
if (f->bytes_written <= f->size)
|
||||
{
|
||||
*f->buffer++ = *bp++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
const struct File_methods MemFile_methods = {
|
||||
&memfile_write,
|
||||
NULL
|
||||
};
|
||||
|
||||
FILE *fmemopen_w(struct MemFile* storage, char *buffer, size_t size)
|
||||
{
|
||||
storage->file.vmt = &MemFile_methods;
|
||||
storage->buffer = buffer;
|
||||
storage->bytes_written = 0;
|
||||
storage->size = size;
|
||||
return (FILE*)storage;
|
||||
}
|
||||
52
arcfw/baselibc/src/memmem.c
Normal file
52
arcfw/baselibc/src/memmem.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* memmem.c
|
||||
*
|
||||
* Find a byte string inside a longer byte string
|
||||
*
|
||||
* This uses the "Not So Naive" algorithm, a very simple but
|
||||
* usually effective algorithm, see:
|
||||
*
|
||||
* http://www-igm.univ-mlv.fr/~lecroq/string/
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
|
||||
{
|
||||
const unsigned char *y = (const unsigned char *)haystack;
|
||||
const unsigned char *x = (const unsigned char *)needle;
|
||||
|
||||
size_t j, k, l;
|
||||
|
||||
if (m > n || !m || !n)
|
||||
return NULL;
|
||||
|
||||
if (1 != m) {
|
||||
if (x[0] == x[1]) {
|
||||
k = 2;
|
||||
l = 1;
|
||||
} else {
|
||||
k = 1;
|
||||
l = 2;
|
||||
}
|
||||
|
||||
j = 0;
|
||||
while (j <= n - m) {
|
||||
if (x[1] != y[j + 1]) {
|
||||
j += k;
|
||||
} else {
|
||||
if (!memcmp(x + 2, y + j + 2, m - 2)
|
||||
&& x[0] == y[j])
|
||||
return (void *)&y[j];
|
||||
j += l;
|
||||
}
|
||||
}
|
||||
} else
|
||||
do {
|
||||
if (*y == *x)
|
||||
return (void *)y;
|
||||
y++;
|
||||
} while (--n);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
36
arcfw/baselibc/src/memmove.c
Normal file
36
arcfw/baselibc/src/memmove.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* memmove.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void *memmove(void *dst, const void *src, size_t n)
|
||||
{
|
||||
const char *p = src;
|
||||
char *q = dst;
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
if (q < p) {
|
||||
asm volatile("cld; rep; movsb"
|
||||
: "+c" (n), "+S"(p), "+D"(q));
|
||||
} else {
|
||||
p += (n - 1);
|
||||
q += (n - 1);
|
||||
asm volatile("std; rep; movsb; cld"
|
||||
: "+c" (n), "+S"(p), "+D"(q));
|
||||
}
|
||||
#else
|
||||
if (q < p) {
|
||||
while (n--) {
|
||||
*q++ = *p++;
|
||||
}
|
||||
} else {
|
||||
p += n;
|
||||
q += n;
|
||||
while (n--) {
|
||||
*--q = *--p;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
19
arcfw/baselibc/src/memrchr.c
Normal file
19
arcfw/baselibc/src/memrchr.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* memrchr.c
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void *memrchr(const void *s, int c, size_t n)
|
||||
{
|
||||
const unsigned char *sp = (const unsigned char *)s + n - 1;
|
||||
|
||||
while (n--) {
|
||||
if (*sp == (unsigned char)c)
|
||||
return (void *)sp;
|
||||
sp--;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
30
arcfw/baselibc/src/memset.c
Normal file
30
arcfw/baselibc/src/memset.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* memset.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void *memset(void *dst, int c, size_t n)
|
||||
{
|
||||
char *q = dst;
|
||||
|
||||
#if defined(__i386__)
|
||||
size_t nl = n >> 2;
|
||||
asm volatile ("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
|
||||
: "+c" (nl), "+D" (q)
|
||||
: "a" ((unsigned char)c * 0x01010101U), "r" (n & 3));
|
||||
#elif defined(__x86_64__)
|
||||
size_t nq = n >> 3;
|
||||
asm volatile ("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
|
||||
:"+c" (nq), "+D" (q)
|
||||
: "a" ((unsigned char)c * 0x0101010101010101U),
|
||||
"r" ((uint32_t) n & 7));
|
||||
#else
|
||||
while (n--) {
|
||||
*q++ = c;
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
24
arcfw/baselibc/src/memswap.c
Normal file
24
arcfw/baselibc/src/memswap.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* memswap()
|
||||
*
|
||||
* Swaps the contents of two nonoverlapping memory areas.
|
||||
* This really could be done faster...
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void memswap(void *m1, void *m2, size_t n)
|
||||
{
|
||||
char *p = m1;
|
||||
char *q = m2;
|
||||
char tmp;
|
||||
|
||||
while (n--) {
|
||||
tmp = *p;
|
||||
*p = *q;
|
||||
*q = tmp;
|
||||
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
}
|
||||
13
arcfw/baselibc/src/mrand48.c
Normal file
13
arcfw/baselibc/src/mrand48.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* mrand48.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern unsigned short __rand48_seed[3]; /* Common with lrand48.c, srand48.c */
|
||||
|
||||
long mrand48(void)
|
||||
{
|
||||
return jrand48(__rand48_seed);
|
||||
}
|
||||
11
arcfw/baselibc/src/nrand48.c
Normal file
11
arcfw/baselibc/src/nrand48.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* nrand48.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
long nrand48(unsigned short* xsubi)
|
||||
{
|
||||
return (long)((uint32_t) jrand48(xsubi) >> 1);
|
||||
}
|
||||
46
arcfw/baselibc/src/qsort.c
Normal file
46
arcfw/baselibc/src/qsort.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* qsort.c
|
||||
*
|
||||
* This is actually combsort. It's an O(n log n) algorithm with
|
||||
* simplicity/small code size being its main virtue.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static inline size_t newgap(size_t gap)
|
||||
{
|
||||
gap = (gap * 10) / 13;
|
||||
if (gap == 9 || gap == 10)
|
||||
gap = 11;
|
||||
|
||||
if (gap < 1)
|
||||
gap = 1;
|
||||
return gap;
|
||||
}
|
||||
|
||||
void qsort(void *base, size_t nmemb, size_t size,
|
||||
int (*compar) (const void *, const void *))
|
||||
{
|
||||
size_t gap = nmemb;
|
||||
size_t i, j;
|
||||
char *p1, *p2;
|
||||
int swapped;
|
||||
|
||||
if (!nmemb)
|
||||
return;
|
||||
|
||||
do {
|
||||
gap = newgap(gap);
|
||||
swapped = 0;
|
||||
|
||||
for (i = 0, p1 = base; i < nmemb - gap; i++, p1 += size) {
|
||||
j = i + gap;
|
||||
if (compar(p1, p2 = (char *)base + j * size) > 0) {
|
||||
memswap(p1, p2, size);
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
} while (gap > 1 || swapped);
|
||||
}
|
||||
48
arcfw/baselibc/src/realloc.c
Normal file
48
arcfw/baselibc/src/realloc.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* realloc.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "malloc.h"
|
||||
|
||||
/* FIXME: This is cheesy, it should be fixed later */
|
||||
|
||||
void *realloc(void *ptr, size_t size)
|
||||
{
|
||||
struct free_arena_header *ah;
|
||||
void *newptr;
|
||||
size_t oldsize;
|
||||
|
||||
if (!ptr)
|
||||
return malloc(size);
|
||||
|
||||
if (size == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Add the obligatory arena header, and round up */
|
||||
size = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
|
||||
|
||||
ah = (struct free_arena_header *)
|
||||
((struct arena_header *)ptr - 1);
|
||||
|
||||
if (ah->a.size >= size && size >= (ah->a.size >> 2)) {
|
||||
/* This field is a good size already. */
|
||||
return ptr;
|
||||
} else {
|
||||
/* Make me a new block. This is kind of bogus; we should
|
||||
be checking the following block to see if we can do an
|
||||
in-place adjustment... fix that later. */
|
||||
|
||||
oldsize = ah->a.size - sizeof(struct arena_header);
|
||||
|
||||
newptr = malloc(size);
|
||||
memcpy(newptr, ptr, (size < oldsize) ? size : oldsize);
|
||||
free(ptr);
|
||||
|
||||
return newptr;
|
||||
}
|
||||
}
|
||||
21
arcfw/baselibc/src/sprintf.c
Normal file
21
arcfw/baselibc/src/sprintf.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* sprintf.c
|
||||
*/
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int sprintf(char *buffer, const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int rv;
|
||||
|
||||
va_start(ap, format);
|
||||
rv = vsnprintf(buffer, INT32_MAX, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
return rv;
|
||||
}
|
||||
#endif
|
||||
15
arcfw/baselibc/src/srand48.c
Normal file
15
arcfw/baselibc/src/srand48.c
Normal file
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* srand48.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
unsigned short __rand48_seed[3]; /* Common with mrand48.c, lrand48.c */
|
||||
|
||||
void srand48(long seedval)
|
||||
{
|
||||
__rand48_seed[0] = 0x330e;
|
||||
__rand48_seed[1] = (unsigned short)seedval;
|
||||
__rand48_seed[2] = (unsigned short)((uint32_t) seedval >> 16);
|
||||
}
|
||||
17
arcfw/baselibc/src/sscanf.c
Normal file
17
arcfw/baselibc/src/sscanf.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* sscanf()
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int sscanf(const char *str, const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int rv;
|
||||
|
||||
va_start(ap, format);
|
||||
rv = vsscanf(str, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
return rv;
|
||||
}
|
||||
24
arcfw/baselibc/src/strcasecmp.c
Normal file
24
arcfw/baselibc/src/strcasecmp.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* strcasecmp.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int strcasecmp(const char *s1, const char *s2)
|
||||
{
|
||||
const unsigned char *c1 = (const unsigned char *)s1;
|
||||
const unsigned char *c2 = (const unsigned char *)s2;
|
||||
unsigned char ch;
|
||||
int d = 0;
|
||||
|
||||
while (1) {
|
||||
/* toupper() expects an unsigned char (implicitly cast to int)
|
||||
as input, and returns an int, which is exactly what we want. */
|
||||
d = toupper(ch = *c1++) - toupper(*c2++);
|
||||
if (d || !ch)
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
11
arcfw/baselibc/src/strcat.c
Normal file
11
arcfw/baselibc/src/strcat.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* strcat.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strcat(char *dst, const char *src)
|
||||
{
|
||||
strcpy(strchr(dst, '\0'), src);
|
||||
return dst;
|
||||
}
|
||||
17
arcfw/baselibc/src/strchr.c
Normal file
17
arcfw/baselibc/src/strchr.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* strchr.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strchr(const char *s, int c)
|
||||
{
|
||||
while (*s != (char)c) {
|
||||
if (!*s)
|
||||
return NULL;
|
||||
s++;
|
||||
}
|
||||
|
||||
return (char *)s;
|
||||
}
|
||||
|
||||
21
arcfw/baselibc/src/strcmp.c
Normal file
21
arcfw/baselibc/src/strcmp.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* strcmp.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
const unsigned char *c1 = (const unsigned char *)s1;
|
||||
const unsigned char *c2 = (const unsigned char *)s2;
|
||||
unsigned char ch;
|
||||
int d = 0;
|
||||
|
||||
while (1) {
|
||||
d = (int)(ch = *c1++) - (int)*c2++;
|
||||
if (d || !ch)
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
20
arcfw/baselibc/src/strcpy.c
Normal file
20
arcfw/baselibc/src/strcpy.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* strcpy.c
|
||||
*
|
||||
* strcpy()
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strcpy(char *dst, const char *src)
|
||||
{
|
||||
char *q = dst;
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
do {
|
||||
*q++ = ch = *p++;
|
||||
} while (ch);
|
||||
|
||||
return dst;
|
||||
}
|
||||
51
arcfw/baselibc/src/strcspn.c
Normal file
51
arcfw/baselibc/src/strcspn.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
FUNCTION
|
||||
<<strcspn>>---count characters not in string
|
||||
|
||||
INDEX
|
||||
strcspn
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
size_t strcspn(const char *<[s1]>, const char *<[s2]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
size_t strcspn(<[s1]>, <[s2]>)
|
||||
char *<[s1]>;
|
||||
char *<[s2]>;
|
||||
|
||||
DESCRIPTION
|
||||
This function computes the length of the initial part of
|
||||
the string pointed to by <[s1]> which consists entirely of
|
||||
characters <[NOT]> from the string pointed to by <[s2]>
|
||||
(excluding the terminating null character).
|
||||
|
||||
RETURNS
|
||||
<<strcspn>> returns the length of the substring found.
|
||||
|
||||
PORTABILITY
|
||||
<<strcspn>> is ANSI C.
|
||||
|
||||
<<strcspn>> requires no supporting OS subroutines.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strcspn(const char *s1, const char *s2)
|
||||
{
|
||||
const char *s = s1;
|
||||
const char *c;
|
||||
|
||||
while (*s1)
|
||||
{
|
||||
for (c = s2; *c; c++)
|
||||
{
|
||||
if (*s1 == *c)
|
||||
break;
|
||||
}
|
||||
if (*c)
|
||||
break;
|
||||
s1++;
|
||||
}
|
||||
|
||||
return s1 - s;
|
||||
}
|
||||
17
arcfw/baselibc/src/strdup.c
Normal file
17
arcfw/baselibc/src/strdup.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* strdup.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *strdup(const char *s)
|
||||
{
|
||||
int l = strlen(s) + 1;
|
||||
char *d = malloc(l);
|
||||
|
||||
if (d)
|
||||
memcpy(d, s, l);
|
||||
|
||||
return d;
|
||||
}
|
||||
30
arcfw/baselibc/src/strlcat.c
Normal file
30
arcfw/baselibc/src/strlcat.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* strlcat.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlcat(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t bytes = 0;
|
||||
char *q = dst;
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
while (bytes < size && *q) {
|
||||
q++;
|
||||
bytes++;
|
||||
}
|
||||
if (bytes == size)
|
||||
return (bytes + strlen(src));
|
||||
|
||||
while ((ch = *p++)) {
|
||||
if (bytes + 1 < size)
|
||||
*q++ = ch;
|
||||
|
||||
bytes++;
|
||||
}
|
||||
|
||||
*q = '\0';
|
||||
return bytes;
|
||||
}
|
||||
26
arcfw/baselibc/src/strlcpy.c
Normal file
26
arcfw/baselibc/src/strlcpy.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* strlcpy.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlcpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t bytes = 0;
|
||||
char *q = dst;
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
while ((ch = *p++)) {
|
||||
if (bytes + 1 < size)
|
||||
*q++ = ch;
|
||||
|
||||
bytes++;
|
||||
}
|
||||
|
||||
/* If size == 0 there is no space for a final null... */
|
||||
if (size)
|
||||
*q = '\0';
|
||||
|
||||
return bytes;
|
||||
}
|
||||
13
arcfw/baselibc/src/strlen.c
Normal file
13
arcfw/baselibc/src/strlen.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* strlen()
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strlen(const char *s)
|
||||
{
|
||||
const char *ss = s;
|
||||
while (*ss)
|
||||
ss++;
|
||||
return ss - s;
|
||||
}
|
||||
24
arcfw/baselibc/src/strncasecmp.c
Normal file
24
arcfw/baselibc/src/strncasecmp.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* strncasecmp.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
int strncasecmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
const unsigned char *c1 = (const unsigned char *)s1;
|
||||
const unsigned char *c2 = (const unsigned char *)s2;
|
||||
unsigned char ch;
|
||||
int d = 0;
|
||||
|
||||
while (n--) {
|
||||
/* toupper() expects an unsigned char (implicitly cast to int)
|
||||
as input, and returns an int, which is exactly what we want. */
|
||||
d = toupper(ch = *c1++) - toupper(*c2++);
|
||||
if (d || !ch)
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
21
arcfw/baselibc/src/strncat.c
Normal file
21
arcfw/baselibc/src/strncat.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* strncat.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strncat(char *dst, const char *src, size_t n)
|
||||
{
|
||||
char *q = strchr(dst, '\0');
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
while (n--) {
|
||||
*q++ = ch = *p++;
|
||||
if (!ch)
|
||||
return dst;
|
||||
}
|
||||
*q = '\0';
|
||||
|
||||
return dst;
|
||||
}
|
||||
21
arcfw/baselibc/src/strncmp.c
Normal file
21
arcfw/baselibc/src/strncmp.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* strncmp.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
const unsigned char *c1 = (const unsigned char *)s1;
|
||||
const unsigned char *c2 = (const unsigned char *)s2;
|
||||
unsigned char ch;
|
||||
int d = 0;
|
||||
|
||||
while (n--) {
|
||||
d = (int)(ch = *c1++) - (int)*c2++;
|
||||
if (d || !ch)
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
24
arcfw/baselibc/src/strncpy.c
Normal file
24
arcfw/baselibc/src/strncpy.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* strncpy.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strncpy(char *dst, const char *src, size_t n)
|
||||
{
|
||||
char *q = dst;
|
||||
const char *p = src;
|
||||
char ch;
|
||||
|
||||
while (n) {
|
||||
n--;
|
||||
*q++ = ch = *p++;
|
||||
if (!ch)
|
||||
break;
|
||||
}
|
||||
|
||||
/* The specs say strncpy() fills the entire buffer with NUL. Sigh. */
|
||||
memset(q, 0, n);
|
||||
|
||||
return dst;
|
||||
}
|
||||
19
arcfw/baselibc/src/strndup.c
Normal file
19
arcfw/baselibc/src/strndup.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* strndup.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *strndup(const char *s, size_t n)
|
||||
{
|
||||
int l = n > strlen(s) ? strlen(s) + 1 : n + 1;
|
||||
char *d = malloc(l);
|
||||
|
||||
if (!d)
|
||||
return NULL;
|
||||
|
||||
memcpy(d, s, l);
|
||||
d[n] = '\0';
|
||||
return d;
|
||||
}
|
||||
18
arcfw/baselibc/src/strnlen.c
Normal file
18
arcfw/baselibc/src/strnlen.c
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* strnlen()
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strnlen(const char *s, size_t maxlen)
|
||||
{
|
||||
const char *ss = s;
|
||||
|
||||
/* Important: the maxlen test must precede the reference through ss;
|
||||
since the byte beyond the maximum may segfault */
|
||||
while ((maxlen > 0) && *ss) {
|
||||
ss++;
|
||||
maxlen--;
|
||||
}
|
||||
return ss - s;
|
||||
}
|
||||
13
arcfw/baselibc/src/strntoimax.c
Normal file
13
arcfw/baselibc/src/strntoimax.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* strntoimax.c
|
||||
*
|
||||
* strntoimax()
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
intmax_t strntoimax(const char *nptr, char **endptr, int base, size_t n)
|
||||
{
|
||||
return (intmax_t) strntoumax(nptr, endptr, base, n);
|
||||
}
|
||||
77
arcfw/baselibc/src/strntoumax.c
Normal file
77
arcfw/baselibc/src/strntoumax.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* strntoumax.c
|
||||
*
|
||||
* The strntoumax() function and associated
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static inline int digitval(int ch)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
return ch - '0';
|
||||
} else if (ch >= 'A' && ch <= 'Z') {
|
||||
return ch - 'A' + 10;
|
||||
} else if (ch >= 'a' && ch <= 'z') {
|
||||
return ch - 'a' + 10;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
|
||||
{
|
||||
int minus = 0;
|
||||
uintmax_t v = 0;
|
||||
int d;
|
||||
|
||||
while (n && isspace((unsigned char)*nptr)) {
|
||||
nptr++;
|
||||
n--;
|
||||
}
|
||||
|
||||
/* Single optional + or - */
|
||||
if (n) {
|
||||
char c = *nptr;
|
||||
if (c == '-' || c == '+') {
|
||||
minus = (c == '-');
|
||||
nptr++;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
||||
if (base == 0) {
|
||||
if (n >= 2 && nptr[0] == '0' &&
|
||||
(nptr[1] == 'x' || nptr[1] == 'X')) {
|
||||
n -= 2;
|
||||
nptr += 2;
|
||||
base = 16;
|
||||
} else if (n >= 1 && nptr[0] == '0') {
|
||||
n--;
|
||||
nptr++;
|
||||
base = 8;
|
||||
} else {
|
||||
base = 10;
|
||||
}
|
||||
} else if (base == 16) {
|
||||
if (n >= 2 && nptr[0] == '0' &&
|
||||
(nptr[1] == 'x' || nptr[1] == 'X')) {
|
||||
n -= 2;
|
||||
nptr += 2;
|
||||
}
|
||||
}
|
||||
|
||||
while (n && (d = digitval(*nptr)) >= 0 && d < base) {
|
||||
v = v * base + d;
|
||||
n--;
|
||||
nptr++;
|
||||
}
|
||||
|
||||
if (endptr)
|
||||
*endptr = (char *)nptr;
|
||||
|
||||
return minus ? -v : v;
|
||||
}
|
||||
55
arcfw/baselibc/src/strpbrk.c
Normal file
55
arcfw/baselibc/src/strpbrk.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
FUNCTION
|
||||
<<strpbrk>>---find characters in string
|
||||
|
||||
INDEX
|
||||
strpbrk
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <string.h>
|
||||
char *strpbrk(const char *<[s1]>, const char *<[s2]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <string.h>
|
||||
char *strpbrk(<[s1]>, <[s2]>)
|
||||
char *<[s1]>;
|
||||
char *<[s2]>;
|
||||
|
||||
DESCRIPTION
|
||||
This function locates the first occurence in the string
|
||||
pointed to by <[s1]> of any character in string pointed to by
|
||||
<[s2]> (excluding the terminating null character).
|
||||
|
||||
RETURNS
|
||||
<<strpbrk>> returns a pointer to the character found in <[s1]>, or a
|
||||
null pointer if no character from <[s2]> occurs in <[s1]>.
|
||||
|
||||
PORTABILITY
|
||||
<<strpbrk>> requires no supporting OS subroutines.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strpbrk(const char *s1, const char *s2)
|
||||
{
|
||||
const char *c = s2;
|
||||
if (!*s1)
|
||||
return (char *) NULL;
|
||||
|
||||
while (*s1)
|
||||
{
|
||||
for (c = s2; *c; c++)
|
||||
{
|
||||
if (*s1 == *c)
|
||||
break;
|
||||
}
|
||||
if (*c)
|
||||
break;
|
||||
s1++;
|
||||
}
|
||||
|
||||
if (*c == '\0')
|
||||
s1 = NULL;
|
||||
|
||||
return (char *) s1;
|
||||
}
|
||||
19
arcfw/baselibc/src/strrchr.c
Normal file
19
arcfw/baselibc/src/strrchr.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* strrchr.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strrchr(const char *s, int c)
|
||||
{
|
||||
const char *found = NULL;
|
||||
|
||||
while (*s) {
|
||||
if (*s == (char)c)
|
||||
found = s;
|
||||
s++;
|
||||
}
|
||||
|
||||
return (char *)found;
|
||||
}
|
||||
|
||||
21
arcfw/baselibc/src/strsep.c
Normal file
21
arcfw/baselibc/src/strsep.c
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* strsep.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strsep(char **stringp, const char *delim)
|
||||
{
|
||||
char *s = *stringp;
|
||||
char *e;
|
||||
|
||||
if (!s)
|
||||
return NULL;
|
||||
|
||||
e = strpbrk(s, delim);
|
||||
if (e)
|
||||
*e++ = '\0';
|
||||
|
||||
*stringp = e;
|
||||
return s;
|
||||
}
|
||||
56
arcfw/baselibc/src/strspn.c
Normal file
56
arcfw/baselibc/src/strspn.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
FUNCTION
|
||||
<<strspn>>---find initial match
|
||||
|
||||
INDEX
|
||||
strspn
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <string.h>
|
||||
size_t strspn(const char *<[s1]>, const char *<[s2]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <string.h>
|
||||
size_t strspn(<[s1]>, <[s2]>)
|
||||
char *<[s1]>;
|
||||
char *<[s2]>;
|
||||
|
||||
DESCRIPTION
|
||||
This function computes the length of the initial segment of
|
||||
the string pointed to by <[s1]> which consists entirely of
|
||||
characters from the string pointed to by <[s2]> (excluding the
|
||||
terminating null character).
|
||||
|
||||
RETURNS
|
||||
<<strspn>> returns the length of the segment found.
|
||||
|
||||
PORTABILITY
|
||||
<<strspn>> is ANSI C.
|
||||
|
||||
<<strspn>> requires no supporting OS subroutines.
|
||||
|
||||
QUICKREF
|
||||
strspn ansi pure
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t strspn(const char *s1, const char *s2)
|
||||
{
|
||||
const char *s = s1;
|
||||
const char *c;
|
||||
|
||||
while (*s1)
|
||||
{
|
||||
for (c = s2; *c; c++)
|
||||
{
|
||||
if (*s1 == *c)
|
||||
break;
|
||||
}
|
||||
if (*c == '\0')
|
||||
break;
|
||||
s1++;
|
||||
}
|
||||
|
||||
return s1 - s;
|
||||
}
|
||||
11
arcfw/baselibc/src/strstr.c
Normal file
11
arcfw/baselibc/src/strstr.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* strstr.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strstr(const char *haystack, const char *needle)
|
||||
{
|
||||
return (char *)memmem(haystack, strlen(haystack), needle,
|
||||
strlen(needle));
|
||||
}
|
||||
3
arcfw/baselibc/src/strtoimax.c
Normal file
3
arcfw/baselibc/src/strtoimax.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE intmax_t
|
||||
#define NAME strtoimax
|
||||
#include "strtox.c"
|
||||
12
arcfw/baselibc/src/strtok.c
Normal file
12
arcfw/baselibc/src/strtok.c
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* strtok.c
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char *strtok(char *s, const char *delim)
|
||||
{
|
||||
static char *holder;
|
||||
|
||||
return strtok_r(s, delim, &holder);
|
||||
}
|
||||
13
arcfw/baselibc/src/strtok_r.c
Normal file
13
arcfw/baselibc/src/strtok_r.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
|
||||
char *strtok_r(char *s, const char *delim, char **holder)
|
||||
{
|
||||
if (s)
|
||||
*holder = s;
|
||||
|
||||
do {
|
||||
s = strsep(holder, delim);
|
||||
} while (s && !*s);
|
||||
|
||||
return s;
|
||||
}
|
||||
3
arcfw/baselibc/src/strtol.c
Normal file
3
arcfw/baselibc/src/strtol.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE signed long
|
||||
#define NAME strtol
|
||||
#include "strtox.c"
|
||||
3
arcfw/baselibc/src/strtoll.c
Normal file
3
arcfw/baselibc/src/strtoll.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE signed long long
|
||||
#define NAME strtoll
|
||||
#include "strtox.c"
|
||||
3
arcfw/baselibc/src/strtoul.c
Normal file
3
arcfw/baselibc/src/strtoul.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE unsigned long
|
||||
#define NAME strtoul
|
||||
#include "strtox.c"
|
||||
3
arcfw/baselibc/src/strtoull.c
Normal file
3
arcfw/baselibc/src/strtoull.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE unsigned long long
|
||||
#define NAME strtoull
|
||||
#include "strtox.c"
|
||||
3
arcfw/baselibc/src/strtoumax.c
Normal file
3
arcfw/baselibc/src/strtoumax.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define TYPE uintmax_t
|
||||
#define NAME strtoumax
|
||||
#include "strtox.c"
|
||||
14
arcfw/baselibc/src/templates/atox.c
Normal file
14
arcfw/baselibc/src/templates/atox.c
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* atox.c
|
||||
*
|
||||
* atoi(), atol(), atoll()
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TYPE NAME(const char *nptr)
|
||||
{
|
||||
return (TYPE) strntoumax(nptr, (char **)NULL, 10, ~(size_t) 0);
|
||||
}
|
||||
14
arcfw/baselibc/src/templates/strtox.c
Normal file
14
arcfw/baselibc/src/templates/strtox.c
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* strtox.c
|
||||
*
|
||||
* strto...() functions, by macro definition
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
TYPE NAME(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
return (TYPE) strntoumax(nptr, endptr, base, ~(size_t) 0);
|
||||
}
|
||||
375
arcfw/baselibc/src/tinyprintf.c
Normal file
375
arcfw/baselibc/src/tinyprintf.c
Normal file
@@ -0,0 +1,375 @@
|
||||
/*
|
||||
File: tinyprintf.c
|
||||
|
||||
Copyright (C) 2004 Kustaa Nyholm
|
||||
Copyright (C) 2010 CJlano
|
||||
Copyright (C) 2011 Petteri Aimonen
|
||||
|
||||
This file is dual-licensed. You can use either of these licenses:
|
||||
|
||||
1) GNU LGPL
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
2) BSD
|
||||
Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of Kustaa Nyholm or SpareTimeLabs nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
/* This is a smaller implementation of printf-family of functions,
|
||||
* based on tinyprintf code by Kustaa Nyholm.
|
||||
* The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
|
||||
* Zero padding and field width are also supported.
|
||||
* If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
|
||||
* long specifier is also supported.
|
||||
* Otherwise it is ignored, so on 32 bit platforms there is no point to use
|
||||
* PRINTF_SUPPORT_LONG because int == long.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct param {
|
||||
int width; /**< field width */
|
||||
char lz; /**< Leading zeros */
|
||||
char sign; /**< The sign to display (if any) */
|
||||
char alt; /**< alternate form */
|
||||
char base; /**< number base (e.g.: 8, 10, 16) */
|
||||
char uc; /**< Upper case (for base16 only) */
|
||||
char *bf; /**< Buffer to output */
|
||||
};
|
||||
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
|
||||
static void uli2a(unsigned long int num, struct param *p)
|
||||
{
|
||||
int n = 0;
|
||||
unsigned long int d = 1;
|
||||
char *bf = p->bf;
|
||||
while (num / d >= p->base)
|
||||
d *= p->base;
|
||||
while (d != 0) {
|
||||
int dgt = num / d;
|
||||
num %= d;
|
||||
d /= p->base;
|
||||
if (n || dgt > 0 || d == 0) {
|
||||
*bf++ = dgt + (dgt < 10 ? '0' : (p->uc ? 'A' : 'a') - 10);
|
||||
++n;
|
||||
}
|
||||
}
|
||||
*bf = 0;
|
||||
}
|
||||
|
||||
static void li2a(long num, struct param *p)
|
||||
{
|
||||
if (num < 0) {
|
||||
num = -num;
|
||||
p->sign = '-';
|
||||
}
|
||||
uli2a(num, p);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void ui2a(unsigned int num, struct param *p)
|
||||
{
|
||||
int n = 0;
|
||||
unsigned int d = 1;
|
||||
char *bf = p->bf;
|
||||
while (num / d >= p->base)
|
||||
d *= p->base;
|
||||
while (d != 0) {
|
||||
int dgt = num / d;
|
||||
num %= d;
|
||||
d /= p->base;
|
||||
if (n || dgt > 0 || d == 0) {
|
||||
*bf++ = dgt + (dgt < 10 ? '0' : (p->uc ? 'A' : 'a') - 10);
|
||||
++n;
|
||||
}
|
||||
}
|
||||
*bf = 0;
|
||||
}
|
||||
|
||||
static void i2a(int num, struct param *p)
|
||||
{
|
||||
if (num < 0) {
|
||||
num = -num;
|
||||
p->sign = '-';
|
||||
}
|
||||
ui2a(num, p);
|
||||
}
|
||||
|
||||
static int a2d(char ch)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
return ch - '0';
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
return ch - 'a' + 10;
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
return ch - 'A' + 10;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
static char a2i(char ch, const char **src, int base, int *nump)
|
||||
{
|
||||
const char *p = *src;
|
||||
int num = 0;
|
||||
int digit;
|
||||
while ((digit = a2d(ch)) >= 0) {
|
||||
if (digit > base)
|
||||
break;
|
||||
num = num * base + digit;
|
||||
ch = *p++;
|
||||
}
|
||||
*src = p;
|
||||
*nump = num;
|
||||
return ch;
|
||||
}
|
||||
|
||||
static int putf(FILE *putp, char c)
|
||||
{
|
||||
// which idiot stubbed this out?
|
||||
putp->vmt->write(putp, &c, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int putsf(FILE *putp, char* str, int len) {
|
||||
putp->vmt->write(putp, str, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned putchw(FILE *putp, struct param *p)
|
||||
{
|
||||
unsigned written = 0;
|
||||
int n = p->width;
|
||||
char *bf = p->bf;
|
||||
|
||||
/* Number of filling characters */
|
||||
while (*bf++ && n > 0)
|
||||
n--;
|
||||
if (p->sign)
|
||||
n--;
|
||||
if (p->alt && p->base == 16)
|
||||
n -= 2;
|
||||
else if (p->alt && p->base == 8)
|
||||
n--;
|
||||
|
||||
/* Fill with space, before alternate or sign */
|
||||
if (!p->lz) {
|
||||
while (n-- > 0)
|
||||
written += putf(putp, ' ');
|
||||
}
|
||||
|
||||
/* print sign */
|
||||
if (p->sign)
|
||||
written += putf(putp, p->sign);
|
||||
|
||||
/* Alternate */
|
||||
if (p->alt && p->base == 16) {
|
||||
written += putf(putp, '0');
|
||||
written += putf(putp, (p->uc ? 'X' : 'x'));
|
||||
} else if (p->alt && p->base == 8) {
|
||||
written += putf(putp, '0');
|
||||
}
|
||||
|
||||
/* Fill with zeros, after alternate or sign */
|
||||
if (p->lz) {
|
||||
while (n-- > 0)
|
||||
written += putf(putp, '0');
|
||||
}
|
||||
|
||||
/* Put actual buffer */
|
||||
bf = p->bf;
|
||||
written += putsf(putp, bf, strlen(bf));
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
size_t tfp_format(FILE *putp, const char *fmt, va_list va)
|
||||
{
|
||||
size_t written = 0;
|
||||
struct param p;
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
char bf[23];
|
||||
#else
|
||||
char bf[12];
|
||||
#endif
|
||||
p.bf = bf;
|
||||
|
||||
char ch;
|
||||
|
||||
while ((ch = *(fmt++))) {
|
||||
if (ch != '%') {
|
||||
written += putf(putp, ch);
|
||||
} else {
|
||||
/* Init parameter struct */
|
||||
p.lz = 0;
|
||||
p.alt = 0;
|
||||
p.width = 0;
|
||||
p.sign = 0;
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
char lng = 0;
|
||||
#endif
|
||||
|
||||
/* Flags */
|
||||
while ((ch = *(fmt++))) {
|
||||
switch (ch) {
|
||||
case '0':
|
||||
p.lz = 1;
|
||||
continue;
|
||||
case '#':
|
||||
p.alt = 1;
|
||||
continue;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Width */
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
ch = a2i(ch, &fmt, 10, &(p.width));
|
||||
}
|
||||
if (ch == 'l') {
|
||||
ch = *(fmt++);
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
lng = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
switch (ch) {
|
||||
case 0:
|
||||
goto abort;
|
||||
case 'u':
|
||||
p.base = 10;
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
if (lng)
|
||||
uli2a(va_arg(va, unsigned long int), &p);
|
||||
else
|
||||
#endif
|
||||
ui2a(va_arg(va, unsigned int), &p);
|
||||
written += putchw(putp, &p);
|
||||
break;
|
||||
case 'd':
|
||||
case 'i':
|
||||
p.base = 10;
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
if (lng)
|
||||
li2a(va_arg(va, unsigned long int), &p);
|
||||
else
|
||||
#endif
|
||||
i2a(va_arg(va, int), &p);
|
||||
written += putchw(putp, &p);
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
p.base = 16;
|
||||
p.uc = (ch == 'X');
|
||||
#ifdef PRINTF_LONG_SUPPORT
|
||||
if (lng)
|
||||
uli2a(va_arg(va, unsigned long int), &p);
|
||||
else
|
||||
#endif
|
||||
ui2a(va_arg(va, unsigned int), &p);
|
||||
written += putchw(putp, &p);
|
||||
break;
|
||||
case 'o':
|
||||
p.base = 8;
|
||||
ui2a(va_arg(va, unsigned int), &p);
|
||||
written += putchw(putp, &p);
|
||||
break;
|
||||
case 'c':
|
||||
written += putf(putp, (char)(va_arg(va, int)));
|
||||
break;
|
||||
case 's':
|
||||
p.bf = va_arg(va, char *);
|
||||
written += putchw(putp, &p);
|
||||
p.bf = bf;
|
||||
break;
|
||||
case '%':
|
||||
written += putf(putp, ch);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
abort:;
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
int vfprintf(FILE *f, const char *fmt, va_list va)
|
||||
{
|
||||
return tfp_format(f, fmt, va);
|
||||
}
|
||||
|
||||
int fprintf(FILE *f, const char *fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
int rv = vfprintf(f, fmt, va);
|
||||
va_end(va);
|
||||
return rv;
|
||||
}
|
||||
|
||||
#ifdef WITH_STDIO
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
int rv = vfprintf(stdout, fmt, va);
|
||||
va_end(va);
|
||||
return rv;
|
||||
}
|
||||
#endif
|
||||
|
||||
int vsnprintf(char *str, size_t size, const char *fmt, va_list va)
|
||||
{
|
||||
struct MemFile state;
|
||||
FILE *f = fmemopen_w(&state, str, size - 1);
|
||||
tfp_format(f, fmt, va);
|
||||
*(state.buffer) = '\0';
|
||||
return state.bytes_written;
|
||||
}
|
||||
|
||||
int snprintf(char *str, size_t size, const char *fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
int rv = vsnprintf(str, size, fmt, va);
|
||||
va_end(va);
|
||||
return rv;
|
||||
}
|
||||
25
arcfw/baselibc/src/vasprintf.c
Normal file
25
arcfw/baselibc/src/vasprintf.c
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* vasprintf.c
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int vasprintf(char **bufp, const char *format, va_list ap)
|
||||
{
|
||||
va_list ap1;
|
||||
int bytes;
|
||||
char *p;
|
||||
|
||||
va_copy(ap1, ap);
|
||||
|
||||
bytes = vsnprintf(NULL, 0, format, ap1) + 1;
|
||||
va_end(ap1);
|
||||
|
||||
*bufp = p = malloc(bytes);
|
||||
if (!p)
|
||||
return -1;
|
||||
|
||||
return vsnprintf(p, bytes, format, ap);
|
||||
}
|
||||
13
arcfw/baselibc/src/vprintf.c
Normal file
13
arcfw/baselibc/src/vprintf.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifdef WITH_STDIO
|
||||
/*
|
||||
* vprintf.c
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int vprintf(const char *format, va_list ap)
|
||||
{
|
||||
return vfprintf(stdout, format, ap);
|
||||
}
|
||||
#endif
|
||||
12
arcfw/baselibc/src/vsprintf.c
Normal file
12
arcfw/baselibc/src/vsprintf.c
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* vsprintf.c
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int vsprintf(char *buffer, const char *format, va_list ap)
|
||||
{
|
||||
return vsnprintf(buffer, INT32_MAX, format, ap);
|
||||
}
|
||||
400
arcfw/baselibc/src/vsscanf.c
Normal file
400
arcfw/baselibc/src/vsscanf.c
Normal file
@@ -0,0 +1,400 @@
|
||||
/*
|
||||
* vsscanf.c
|
||||
*
|
||||
* vsscanf(), from which the rest of the scanf()
|
||||
* family is built
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef LONG_BIT
|
||||
#define LONG_BIT (CHAR_BIT*sizeof(long))
|
||||
#endif
|
||||
|
||||
enum flags {
|
||||
FL_SPLAT = 0x01, /* Drop the value, do not assign */
|
||||
FL_INV = 0x02, /* Character-set with inverse */
|
||||
FL_WIDTH = 0x04, /* Field width specified */
|
||||
FL_MINUS = 0x08, /* Negative number */
|
||||
};
|
||||
|
||||
enum ranks {
|
||||
rank_char = -2,
|
||||
rank_short = -1,
|
||||
rank_int = 0,
|
||||
rank_long = 1,
|
||||
rank_longlong = 2,
|
||||
rank_ptr = INT_MAX /* Special value used for pointers */
|
||||
};
|
||||
|
||||
#define MIN_RANK rank_char
|
||||
#define MAX_RANK rank_longlong
|
||||
|
||||
#define INTMAX_RANK rank_longlong
|
||||
#define SIZE_T_RANK rank_long
|
||||
#define PTRDIFF_T_RANK rank_long
|
||||
|
||||
enum bail {
|
||||
bail_none = 0, /* No error condition */
|
||||
bail_eof, /* Hit EOF */
|
||||
bail_err /* Conversion mismatch */
|
||||
};
|
||||
|
||||
static inline const char *skipspace(const char *p)
|
||||
{
|
||||
while (isspace((unsigned char)*p))
|
||||
p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
#undef set_bit
|
||||
static inline void set_bit(unsigned long *bitmap, unsigned int bit)
|
||||
{
|
||||
bitmap[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
|
||||
}
|
||||
|
||||
#undef test_bit
|
||||
static inline int test_bit(unsigned long *bitmap, unsigned int bit)
|
||||
{
|
||||
return (int)(bitmap[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1;
|
||||
}
|
||||
|
||||
int vsscanf(const char *buffer, const char *format, va_list ap)
|
||||
{
|
||||
const char *p = format;
|
||||
char ch;
|
||||
unsigned char uc;
|
||||
const char *q = buffer;
|
||||
const char *qq;
|
||||
uintmax_t val = 0;
|
||||
int rank = rank_int; /* Default rank */
|
||||
unsigned int width = UINT_MAX;
|
||||
int base;
|
||||
enum flags flags = 0;
|
||||
enum {
|
||||
st_normal, /* Ground state */
|
||||
st_flags, /* Special flags */
|
||||
st_width, /* Field width */
|
||||
st_modifiers, /* Length or conversion modifiers */
|
||||
st_match_init, /* Initial state of %[ sequence */
|
||||
st_match, /* Main state of %[ sequence */
|
||||
st_match_range, /* After - in a %[ sequence */
|
||||
} state = st_normal;
|
||||
char *sarg = NULL; /* %s %c or %[ string argument */
|
||||
enum bail bail = bail_none;
|
||||
int sign;
|
||||
int converted = 0; /* Successful conversions */
|
||||
unsigned long matchmap[((1 << CHAR_BIT) + (LONG_BIT - 1)) / LONG_BIT];
|
||||
int matchinv = 0; /* Is match map inverted? */
|
||||
unsigned char range_start = 0;
|
||||
(void)sign;
|
||||
|
||||
while ((ch = *p++) && !bail) {
|
||||
switch (state) {
|
||||
case st_normal:
|
||||
if (ch == '%') {
|
||||
state = st_flags;
|
||||
flags = 0;
|
||||
rank = rank_int;
|
||||
width = UINT_MAX;
|
||||
} else if (isspace((unsigned char)ch)) {
|
||||
q = skipspace(q);
|
||||
} else {
|
||||
if (*q == ch)
|
||||
q++;
|
||||
else
|
||||
bail = bail_err; /* Match failure */
|
||||
}
|
||||
break;
|
||||
|
||||
case st_flags:
|
||||
switch (ch) {
|
||||
case '*':
|
||||
flags |= FL_SPLAT;
|
||||
break;
|
||||
case '0'...'9':
|
||||
width = (ch - '0');
|
||||
state = st_width;
|
||||
flags |= FL_WIDTH;
|
||||
break;
|
||||
default:
|
||||
state = st_modifiers;
|
||||
p--; /* Process this character again */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case st_width:
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
width = width * 10 + (ch - '0');
|
||||
} else {
|
||||
state = st_modifiers;
|
||||
p--; /* Process this character again */
|
||||
}
|
||||
break;
|
||||
|
||||
case st_modifiers:
|
||||
switch (ch) {
|
||||
/* Length modifiers - nonterminal sequences */
|
||||
case 'h':
|
||||
rank--; /* Shorter rank */
|
||||
break;
|
||||
case 'l':
|
||||
rank++; /* Longer rank */
|
||||
break;
|
||||
case 'j':
|
||||
rank = INTMAX_RANK;
|
||||
break;
|
||||
case 'z':
|
||||
rank = SIZE_T_RANK;
|
||||
break;
|
||||
case 't':
|
||||
rank = PTRDIFF_T_RANK;
|
||||
break;
|
||||
case 'L':
|
||||
case 'q':
|
||||
rank = rank_longlong; /* long double/long long */
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Output modifiers - terminal sequences */
|
||||
/* Next state will be normal */
|
||||
state = st_normal;
|
||||
|
||||
/* Canonicalize rank */
|
||||
if (rank < MIN_RANK)
|
||||
rank = MIN_RANK;
|
||||
else if (rank > MAX_RANK)
|
||||
rank = MAX_RANK;
|
||||
|
||||
switch (ch) {
|
||||
case 'P': /* Upper case pointer */
|
||||
case 'p': /* Pointer */
|
||||
rank = rank_ptr;
|
||||
base = 0;
|
||||
sign = 0;
|
||||
goto scan_int;
|
||||
|
||||
case 'i': /* Base-independent integer */
|
||||
base = 0;
|
||||
sign = 1;
|
||||
goto scan_int;
|
||||
|
||||
case 'd': /* Decimal integer */
|
||||
base = 10;
|
||||
sign = 1;
|
||||
goto scan_int;
|
||||
|
||||
case 'o': /* Octal integer */
|
||||
base = 8;
|
||||
sign = 0;
|
||||
goto scan_int;
|
||||
|
||||
case 'u': /* Unsigned decimal integer */
|
||||
base = 10;
|
||||
sign = 0;
|
||||
goto scan_int;
|
||||
|
||||
case 'x': /* Hexadecimal integer */
|
||||
case 'X':
|
||||
base = 16;
|
||||
sign = 0;
|
||||
goto scan_int;
|
||||
|
||||
case 'n': /* # of characters consumed */
|
||||
val = (q - buffer);
|
||||
goto set_integer;
|
||||
|
||||
scan_int:
|
||||
q = skipspace(q);
|
||||
if (!*q) {
|
||||
bail = bail_eof;
|
||||
break;
|
||||
}
|
||||
val =
|
||||
strntoumax(q, (char **)&qq, base,
|
||||
width);
|
||||
if (qq == q) {
|
||||
bail = bail_err;
|
||||
break;
|
||||
}
|
||||
q = qq;
|
||||
if (!(flags & FL_SPLAT))
|
||||
converted++;
|
||||
/* fall through */
|
||||
|
||||
set_integer:
|
||||
if (!(flags & FL_SPLAT)) {
|
||||
switch (rank) {
|
||||
case rank_char:
|
||||
*va_arg(ap,
|
||||
unsigned char *)
|
||||
= val;
|
||||
break;
|
||||
case rank_short:
|
||||
*va_arg(ap,
|
||||
unsigned short
|
||||
*) = val;
|
||||
break;
|
||||
case rank_int:
|
||||
*va_arg(ap,
|
||||
unsigned int *)
|
||||
= val;
|
||||
break;
|
||||
case rank_long:
|
||||
*va_arg(ap,
|
||||
unsigned long *)
|
||||
= val;
|
||||
break;
|
||||
case rank_longlong:
|
||||
*va_arg(ap,
|
||||
unsigned long
|
||||
long *) = val;
|
||||
break;
|
||||
case rank_ptr:
|
||||
*va_arg(ap, void **) =
|
||||
(void *)
|
||||
(uintptr_t)val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'c': /* Character */
|
||||
/* Default width == 1 */
|
||||
width = (flags & FL_WIDTH) ? width : 1;
|
||||
if (flags & FL_SPLAT) {
|
||||
while (width--) {
|
||||
if (!*q) {
|
||||
bail = bail_eof;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sarg = va_arg(ap, char *);
|
||||
while (width--) {
|
||||
if (!*q) {
|
||||
bail = bail_eof;
|
||||
break;
|
||||
}
|
||||
*sarg++ = *q++;
|
||||
}
|
||||
if (!bail)
|
||||
converted++;
|
||||
}
|
||||
break;
|
||||
|
||||
case 's': /* String */
|
||||
uc = 1; /* Anything nonzero */
|
||||
if (flags & FL_SPLAT) {
|
||||
while (width-- && (uc = *q) &&
|
||||
!isspace(uc)) {
|
||||
q++;
|
||||
}
|
||||
} else {
|
||||
char *sp;
|
||||
sp = sarg = va_arg(ap, char *);
|
||||
while (width-- && (uc = *q) &&
|
||||
!isspace(uc)) {
|
||||
*sp++ = uc;
|
||||
q++;
|
||||
}
|
||||
if (sarg != sp) {
|
||||
/* Terminate output */
|
||||
*sp = '\0';
|
||||
converted++;
|
||||
}
|
||||
}
|
||||
if (!uc)
|
||||
bail = bail_eof;
|
||||
break;
|
||||
|
||||
case '[': /* Character range */
|
||||
sarg = (flags & FL_SPLAT) ? NULL
|
||||
: va_arg(ap, char *);
|
||||
state = st_match_init;
|
||||
matchinv = 0;
|
||||
memset(matchmap, 0, sizeof matchmap);
|
||||
break;
|
||||
|
||||
case '%': /* %% sequence */
|
||||
if (*q == '%')
|
||||
q++;
|
||||
else
|
||||
bail = bail_err;
|
||||
break;
|
||||
|
||||
default: /* Anything else */
|
||||
/* Unknown sequence */
|
||||
bail = bail_err;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case st_match_init: /* Initial state for %[ match */
|
||||
if (ch == '^' && !(flags & FL_INV)) {
|
||||
matchinv = 1;
|
||||
} else {
|
||||
set_bit(matchmap, (unsigned char)ch);
|
||||
state = st_match;
|
||||
}
|
||||
break;
|
||||
|
||||
case st_match: /* Main state for %[ match */
|
||||
if (ch == ']') {
|
||||
goto match_run;
|
||||
} else if (ch == '-') {
|
||||
range_start = (unsigned char)ch;
|
||||
state = st_match_range;
|
||||
} else {
|
||||
set_bit(matchmap, (unsigned char)ch);
|
||||
}
|
||||
break;
|
||||
|
||||
case st_match_range: /* %[ match after - */
|
||||
if (ch == ']') {
|
||||
/* - was last character */
|
||||
set_bit(matchmap, (unsigned char)'-');
|
||||
goto match_run;
|
||||
} else {
|
||||
int i;
|
||||
for (i = range_start; i < (unsigned char)ch;
|
||||
i++)
|
||||
set_bit(matchmap, i);
|
||||
state = st_match;
|
||||
}
|
||||
break;
|
||||
|
||||
match_run: /* Match expression finished */
|
||||
qq = q;
|
||||
uc = 1; /* Anything nonzero */
|
||||
while (width && (uc = *q)
|
||||
&& test_bit(matchmap, uc)^matchinv) {
|
||||
if (sarg)
|
||||
*sarg++ = uc;
|
||||
q++;
|
||||
}
|
||||
if (q != qq && sarg) {
|
||||
*sarg = '\0';
|
||||
converted++;
|
||||
} else {
|
||||
bail = bail_err;
|
||||
}
|
||||
if (!uc)
|
||||
bail = bail_eof;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (bail == bail_eof && !converted)
|
||||
converted = -1; /* Return EOF (-1) */
|
||||
|
||||
return converted;
|
||||
}
|
||||
22
arcfw/baselibc/tests/.printf_tests.c~
Normal file
22
arcfw/baselibc/tests/.printf_tests.c~
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "unittests.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
int status = 0;
|
||||
|
||||
{
|
||||
COMMENT("Testing basic snprintf functionality");
|
||||
char buf[20];
|
||||
|
||||
snprintf(buf, sizeof(buf), "%08d", 55);
|
||||
TEST(strcmp(buf, "00000055") == 0);
|
||||
|
||||
TEST(snprintf(buf, sizeof(buf), "01234567890123456789") == 19);
|
||||
TEST(strcmp(buf, "0123456789012345678") == 0);
|
||||
}
|
||||
|
||||
if (status != 0)
|
||||
fprintf(stdout, "\n\nSome tests FAILED!\n");
|
||||
|
||||
return status;
|
||||
}
|
||||
33
arcfw/baselibc/tests/.tests_glue.c~
Normal file
33
arcfw/baselibc/tests/.tests_glue.c~
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__USE_POSIX)
|
||||
/* Connects the baselibc stdio to normal POSIX stdio */
|
||||
size_t write(int fd, const void *buf, size_t count);
|
||||
|
||||
static size_t stdio_write(FILE *instance, const char *bp, size_t n)
|
||||
{
|
||||
if (instance == stdout)
|
||||
return write(1, bp, n);
|
||||
else
|
||||
return write(2, bp, n);
|
||||
}
|
||||
#else
|
||||
#error No suitable write() implementation.
|
||||
#endif
|
||||
|
||||
|
||||
static struct File_methods stdio_methods = {
|
||||
&stdio_write, NULL
|
||||
};
|
||||
|
||||
static struct File _stdout = {
|
||||
&stdio_methods
|
||||
};
|
||||
|
||||
static struct File _stderr = {
|
||||
&stdio_methods
|
||||
};
|
||||
|
||||
FILE* const stdout = &_stdout;
|
||||
FILE* const stderr = &_stderr;
|
||||
|
||||
22
arcfw/baselibc/tests/printf_tests.c
Normal file
22
arcfw/baselibc/tests/printf_tests.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "unittests.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
int status = 0;
|
||||
|
||||
{
|
||||
COMMENT("Testing basic snprintf functionality");
|
||||
char buf[20];
|
||||
|
||||
snprintf(buf, sizeof(buf), "%08d", 55);
|
||||
TEST(strcmp(buf, "00000055") == 0);
|
||||
|
||||
TEST(snprintf(buf, sizeof(buf), "01234567890123456789") == 20);
|
||||
TEST(strcmp(buf, "0123456789012345678") == 0);
|
||||
}
|
||||
|
||||
if (status != 0)
|
||||
fprintf(stdout, "\n\nSome tests FAILED!\n");
|
||||
|
||||
return status;
|
||||
}
|
||||
33
arcfw/baselibc/tests/tests_glue.c
Normal file
33
arcfw/baselibc/tests/tests_glue.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(linux)
|
||||
/* Connects the baselibc stdio to normal POSIX stdio */
|
||||
size_t write(int fd, const void *buf, size_t count);
|
||||
|
||||
static size_t stdio_write(FILE *instance, const char *bp, size_t n)
|
||||
{
|
||||
if (instance == stdout)
|
||||
return write(1, bp, n);
|
||||
else
|
||||
return write(2, bp, n);
|
||||
}
|
||||
#else
|
||||
#error No suitable write() implementation.
|
||||
#endif
|
||||
|
||||
|
||||
static struct File_methods stdio_methods = {
|
||||
&stdio_write, NULL
|
||||
};
|
||||
|
||||
static struct File _stdout = {
|
||||
&stdio_methods
|
||||
};
|
||||
|
||||
static struct File _stderr = {
|
||||
&stdio_methods
|
||||
};
|
||||
|
||||
FILE* const stdout = &_stdout;
|
||||
FILE* const stderr = &_stderr;
|
||||
|
||||
14
arcfw/baselibc/tests/unittests.h
Normal file
14
arcfw/baselibc/tests/unittests.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define COMMENT(x) printf("\n----" x "----\n");
|
||||
#define STR(x) #x
|
||||
#define STR2(x) STR(x)
|
||||
#define TEST(x) \
|
||||
if (!(x)) { \
|
||||
fprintf(stderr, "\033[31;1mFAILED:\033[22;39m " __FILE__ ":" STR2(__LINE__) " " #x "\n"); \
|
||||
status = 1; \
|
||||
} else { \
|
||||
printf("\033[32;1mOK:\033[22;39m " #x "\n"); \
|
||||
}
|
||||
|
||||
|
||||
0
arcfw/gccle/dummy
Normal file
0
arcfw/gccle/dummy
Normal file
27
arcfw/of.ld
Normal file
27
arcfw/of.ld
Normal file
@@ -0,0 +1,27 @@
|
||||
OUTPUT_FORMAT("elf32-powerpcle")
|
||||
OUTPUT_ARCH(powerpc:common)
|
||||
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS {
|
||||
. = 0x80900000;
|
||||
__executable_start = .;
|
||||
|
||||
.text : ALIGN(8) { *(.text) *(.text.*) }
|
||||
|
||||
.rodata : ALIGN(8) { *(.rodata) *(.rodata.*) }
|
||||
|
||||
.sdata : ALIGN(8) { *(.sdata) *(.sdata.*) }
|
||||
.data : ALIGN(8) { *(.data) *(.data.*) }
|
||||
|
||||
. = ALIGN(32);
|
||||
__self_end = .;
|
||||
|
||||
__bss_start = .;
|
||||
.bss : ALIGN(8) { *(.bss .bss.*) } :NONE = 0
|
||||
.sbss : { *(.sbss) }
|
||||
__bss_end = .;
|
||||
|
||||
. = ALIGN(32);
|
||||
_end = .;
|
||||
}
|
||||
1015
arcfw/source/arc.h
Normal file
1015
arcfw/source/arc.h
Normal file
File diff suppressed because it is too large
Load Diff
789
arcfw/source/arcconfig.c
Normal file
789
arcfw/source/arcconfig.c
Normal file
@@ -0,0 +1,789 @@
|
||||
#include <stddef.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "arc.h"
|
||||
#include "arcconfig.h"
|
||||
#include "arcdevice.h"
|
||||
#include "arcenv.h"
|
||||
#include "arcconsole.h"
|
||||
#include "arcmem.h"
|
||||
#include "arcio.h"
|
||||
#include "arcdisk.h"
|
||||
#include "runtime.h"
|
||||
|
||||
enum {
|
||||
MAXIMUM_DEVICE_COUNT = 256
|
||||
};
|
||||
|
||||
// Declare stub routines
|
||||
static ARC_STATUS StubOpen(PCHAR OpenPath, OPEN_MODE OpenMode, PULONG FileId) { return _ESUCCESS; }
|
||||
static ARC_STATUS StubClose(ULONG FileId) {
|
||||
// TODO: set closed flag in file table
|
||||
return _ESUCCESS;
|
||||
}
|
||||
static ARC_STATUS StubMount(PCHAR MountPath, MOUNT_OPERATION Operation) { return _EINVAL; }
|
||||
static ARC_STATUS StubSeek(ULONG FileId, PLARGE_INTEGER Offset, SEEK_MODE SeekMode) { return _ESUCCESS; }
|
||||
static ARC_STATUS StubRead(ULONG FileId, PVOID Buffer, ULONG Length, PULONG Count) { return _ESUCCESS; }
|
||||
static ARC_STATUS StubWrite(ULONG FileId, PVOID Buffer, ULONG Length, PULONG Count) { return _ESUCCESS; }
|
||||
static ARC_STATUS StubGetReadStatus(ULONG FileId) { return _ESUCCESS; }
|
||||
static ARC_STATUS StubGetFileInformation(ULONG FileId, PFILE_INFORMATION FileInfo) { return _EINVAL; }
|
||||
|
||||
// Declare display write routine
|
||||
static ARC_STATUS DisplayWrite(ULONG FileId, PVOID Buffer, ULONG Length, PULONG Count) {
|
||||
*Count = ArcConsoleWrite((PBYTE)Buffer, Length);
|
||||
return _ESUCCESS;
|
||||
}
|
||||
|
||||
static const CHAR MonitorPath[] = "multi(0)video(0)monitor(0)";
|
||||
static const CHAR s_DisplayIdentifier[] = "FLIPPER_GX";
|
||||
|
||||
static const DEVICE_VECTORS MonitorVectors = {
|
||||
.Open = StubOpen,
|
||||
.Close = StubClose,
|
||||
.Mount = StubMount,
|
||||
.Read = StubRead,
|
||||
.Write = DisplayWrite,
|
||||
.Seek = StubSeek,
|
||||
.GetReadStatus = StubGetReadStatus,
|
||||
.GetFileInformation = StubGetFileInformation,
|
||||
.SetFileInformation = NULL,
|
||||
.GetDirectoryEntry = NULL
|
||||
};
|
||||
|
||||
// Let printf work.
|
||||
static size_t StdoutWrite(FILE* Instance, const char* bp, size_t n) {
|
||||
return ArcConsoleWrite(bp, n);
|
||||
}
|
||||
static struct File_methods s_fmStdout = {
|
||||
StdoutWrite, NULL
|
||||
};
|
||||
static FILE s_fStdout = { &s_fmStdout };
|
||||
FILE* stdout = &s_fStdout;
|
||||
|
||||
// Declare keyboard routines
|
||||
static ARC_STATUS KeyRead(ULONG FileId, PVOID Buffer, ULONG Length, PULONG Count) {
|
||||
// Read Length chars into buffer, blocking if needed.
|
||||
PUCHAR Buffer8 = (PUCHAR)Buffer;
|
||||
ULONG RealCount = 0;
|
||||
for (; RealCount < Length; RealCount++) {
|
||||
Buffer8[RealCount] = IOSKBD_ReadChar();
|
||||
}
|
||||
*Count = Length;
|
||||
return _ESUCCESS;
|
||||
}
|
||||
|
||||
static ARC_STATUS KeyGetReadStatus(ULONG FileId) {
|
||||
// return EAGAIN if no chars available, otherwise return ESUCCESS
|
||||
return IOSKBD_CharAvailable() ? _ESUCCESS : _EAGAIN;
|
||||
}
|
||||
|
||||
static const CHAR KeyboardPath[] = "multi(0)keyboard(0)";
|
||||
|
||||
static const DEVICE_VECTORS KeyboardVectors = {
|
||||
.Open = StubOpen,
|
||||
.Close = StubClose,
|
||||
.Mount = StubMount,
|
||||
.Read = KeyRead,
|
||||
.Write = StubWrite,
|
||||
.Seek = StubSeek,
|
||||
.GetReadStatus = KeyGetReadStatus,
|
||||
.GetFileInformation = StubGetFileInformation,
|
||||
.SetFileInformation = NULL,
|
||||
.GetDirectoryEntry = NULL
|
||||
};
|
||||
|
||||
// ARC path names.
|
||||
static const PCHAR DeviceTable[] = {
|
||||
"arc",
|
||||
"cpu",
|
||||
"fpu",
|
||||
"pic",
|
||||
"pdc",
|
||||
"sic",
|
||||
"sdc",
|
||||
"sc",
|
||||
"eisa",
|
||||
"tc",
|
||||
"scsi",
|
||||
"dti",
|
||||
"multi",
|
||||
"disk",
|
||||
"tape",
|
||||
"cdrom",
|
||||
"worm",
|
||||
"serial",
|
||||
"net",
|
||||
"video",
|
||||
"par",
|
||||
"point",
|
||||
"key",
|
||||
"audio",
|
||||
"other",
|
||||
"rdisk",
|
||||
"fdisk",
|
||||
"tape",
|
||||
"modem",
|
||||
"monitor",
|
||||
"print",
|
||||
"pointer",
|
||||
"keyboard",
|
||||
"term",
|
||||
"other",
|
||||
"line",
|
||||
"network",
|
||||
"system",
|
||||
"maximum",
|
||||
"partition"
|
||||
};
|
||||
|
||||
static DEVICE_ENTRY Root;
|
||||
static DEVICE_ENTRY Cpu;
|
||||
static DEVICE_ENTRY Flipper;
|
||||
static DEVICE_ENTRY Vegas;
|
||||
static DEVICE_ENTRY Vi;
|
||||
static DEVICE_ENTRY Broadway;
|
||||
|
||||
// USB mass storage
|
||||
static DEVICE_ENTRY UsbDisk = {
|
||||
.Component = ARC_MAKE_COMPONENT(AdapterClass, ScsiAdapter, ARC_DEVICE_INPUT | ARC_DEVICE_OUTPUT, 0, 0),
|
||||
.Parent = &Root
|
||||
};
|
||||
|
||||
// Dummy scsi adapter so setupldr asks for mass storage drivers on flipper systems
|
||||
static char s_ScsiDummyIdentifier[] = "DUMMY";
|
||||
static DEVICE_ENTRY ScsiDummy = {
|
||||
.Component = ARC_MAKE_COMPONENT(AdapterClass, ScsiAdapter, ARC_DEVICE_INPUT | ARC_DEVICE_OUTPUT, 1337, 0),
|
||||
.Parent = &Root
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Monitor
|
||||
static DEVICE_ENTRY Monitor = {
|
||||
.Component = ARC_MAKE_COMPONENT(PeripheralClass, MonitorPeripheral, ARC_DEVICE_OUTPUT | ARC_DEVICE_CONSOLE_OUT, 0, 0),
|
||||
.Parent = &Vi,
|
||||
.Vectors = &MonitorVectors
|
||||
};
|
||||
|
||||
static const ARC_RESOURCE_LIST(ViDesc,
|
||||
ARC_RESOURCE_DESCRIPTOR_INTERRUPT(CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE, 24),
|
||||
ARC_RESOURCE_DESCRIPTOR_PORT(CM_RESOURCE_PORT_MEMORY, 0x0C002000, 0x100)
|
||||
);
|
||||
|
||||
// Video Interface
|
||||
static DEVICE_ENTRY Vi = {
|
||||
.Component = ARC_MAKE_COMPONENT(ControllerClass, DisplayController, ARC_DEVICE_OUTPUT | ARC_DEVICE_CONSOLE_OUT, 0, sizeof(ViDesc)),
|
||||
.Parent = &Flipper,
|
||||
.Child = &Monitor,
|
||||
.ConfigData = &ViDesc.Header,
|
||||
};
|
||||
|
||||
// Keyboard (put this under Flipper despite it could be on Vegas USB too, NT doesn't care)
|
||||
static DEVICE_ENTRY Keyboard = {
|
||||
.Component = ARC_MAKE_COMPONENT(PeripheralClass, KeyboardPeripheral, ARC_DEVICE_INPUT | ARC_DEVICE_CONSOLE_IN, 0, 0),
|
||||
.Parent = &Flipper,
|
||||
.Peer = &Vi,
|
||||
.Vectors = &KeyboardVectors
|
||||
};
|
||||
|
||||
// Disk or CDROM controllers (SD/IDE on EXI bus) go directly underneath Flipper. (Same for "floppy drives" aka drivers.img on those devices)
|
||||
// Format would be disk(exi_disk_index) or cdrom(exi_disk_index) followed by peripheral(image_on_disK)
|
||||
|
||||
// We need some valid bus for some NT driver classes to work at all.
|
||||
// VME bus is allowed here, and nothing actually uses it. Perfect for us :)
|
||||
static char s_FlipperBusIdentifier[] = "VME";
|
||||
|
||||
// Flipper components (at physical address 0C00_0000)
|
||||
static DEVICE_ENTRY Flipper = {
|
||||
.Component = ARC_MAKE_COMPONENT(AdapterClass, MultiFunctionAdapter, ARC_DEVICE_NONE, 0, 0),
|
||||
.Parent = &Root,
|
||||
.Peer = &Vegas,
|
||||
.Child = &Keyboard
|
||||
};
|
||||
|
||||
// Vegas descriptors (IPC interrupt/mem-area/mmio, etc)
|
||||
static ARC_RESOURCE_LIST(VegasDesc,
|
||||
ARC_RESOURCE_DESCRIPTOR_INTERRUPT(CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE, 27), // IOP IPC interrupt
|
||||
ARC_RESOURCE_DESCRIPTOR_PORT(CM_RESOURCE_PORT_MEMORY, 0x0D000000, 0x10), // IPC memory-mapped IO
|
||||
ARC_RESOURCE_DESCRIPTOR_PORT(CM_RESOURCE_PORT_MEMORY, 0x133E0000, 0x2000), // IPC buffers. Needs patching at runtime, this is the expected area where IOS uses 12MB.
|
||||
);
|
||||
|
||||
// Vegas components (at physical address 0D00_0000)
|
||||
// This includes IOS IPC, as IOP (Starlet) is part of Vegas.
|
||||
static DEVICE_ENTRY Vegas = {
|
||||
.Component = ARC_MAKE_COMPONENT(AdapterClass, MultiFunctionAdapter, ARC_DEVICE_NONE, 1, sizeof(VegasDesc)),
|
||||
.Parent = &Root,
|
||||
.Peer = &UsbDisk,
|
||||
.ConfigData = &VegasDesc.Header
|
||||
};
|
||||
|
||||
// PPC750 caches.
|
||||
// For caches, Key definition
|
||||
// bit 0-15: log2(cache size in 4K pages)
|
||||
// bit 16-23: log2(line size in bytes/tag)
|
||||
// bit 24-31: number of cache lines transferred in a cache refill
|
||||
static DEVICE_ENTRY L2Cache = {
|
||||
.Component = ARC_MAKE_COMPONENT(CacheClass, SecondaryCache, ARC_DEVICE_NONE, 0x1060006, 0),
|
||||
.Parent = &Cpu,
|
||||
};
|
||||
static DEVICE_ENTRY Dcache = {
|
||||
.Component = ARC_MAKE_COMPONENT(CacheClass, PrimaryDcache, ARC_DEVICE_NONE, 0x1060003, 0),
|
||||
.Parent = &Cpu,
|
||||
.Peer = &L2Cache
|
||||
};
|
||||
static DEVICE_ENTRY Icache = {
|
||||
.Component = ARC_MAKE_COMPONENT(CacheClass, PrimaryIcache, ARC_DEVICE_NONE, 0x1060003, 0),
|
||||
.Parent = &Cpu,
|
||||
.Peer = &Dcache
|
||||
};
|
||||
|
||||
static const char s_GekkoIdentifier[] = "Gekko";
|
||||
static const char s_BroadwayIdentifier[] = "Broadway";
|
||||
static const char s_EspressoIdentifier[] = "Espresso";
|
||||
|
||||
// CPU
|
||||
// We have to use 613 here for NT-related reasons, this is the preliminary name of the 750.
|
||||
static DEVICE_ENTRY Cpu = {
|
||||
.Component = ARC_MAKE_COMPONENT(ProcessorClass, CentralProcessor, ARC_DEVICE_NONE, 613, 0),
|
||||
.Parent = &Root,
|
||||
.Child = &Icache,
|
||||
.Peer = &Flipper,
|
||||
};
|
||||
|
||||
static const char s_RootIdentifierFlipper[] = "Nintendo GameCube";
|
||||
static const char s_RootIdentifierVegas[] = "Nintendo Wii";
|
||||
static const char s_RootIdentifierLatte[] = "Nintendo Wii U";
|
||||
|
||||
// Root
|
||||
static DEVICE_ENTRY Root = {
|
||||
.Component = ARC_MAKE_COMPONENT(SystemClass, ArcSystem, ARC_DEVICE_NONE, 0, 0),
|
||||
.Child = &Cpu
|
||||
};
|
||||
|
||||
// BUGBUG: If extra default components are added, add them to this list.
|
||||
static PDEVICE_ENTRY s_DefaultComponents[] = {
|
||||
// Root
|
||||
&Root,
|
||||
|
||||
// First level
|
||||
&Cpu,
|
||||
&Flipper,
|
||||
&Vegas,
|
||||
&UsbDisk,
|
||||
&ScsiDummy,
|
||||
|
||||
// Cpu
|
||||
&Icache,
|
||||
&Dcache,
|
||||
&L2Cache,
|
||||
|
||||
// Flipper
|
||||
&Keyboard,
|
||||
&Vi,
|
||||
|
||||
// Video
|
||||
&Monitor,
|
||||
|
||||
// Vegas
|
||||
// (nothing under vegas by default)
|
||||
};
|
||||
|
||||
// Space for additional components.
|
||||
static DEVICE_ENTRY g_AdditionalComponents[MAXIMUM_DEVICE_COUNT] = { 0 };
|
||||
static BYTE g_ConfigurationDataBuffer[MAXIMUM_DEVICE_COUNT * 0x100] = {0};
|
||||
static ULONG g_AdditionalComponentsCount = 0;
|
||||
static ULONG g_ConfigurationDataOffset = 0;
|
||||
_Static_assert(sizeof(DEVICE_ENTRY) < 0x100);
|
||||
|
||||
// Config functions implementation.
|
||||
|
||||
static bool DeviceEntryIsValidImpl(PCONFIGURATION_COMPONENT Component, PULONG Index) {
|
||||
bool ValueForDefault = true;
|
||||
if (Index != NULL) {
|
||||
*Index = -1;
|
||||
ValueForDefault = false;
|
||||
}
|
||||
if (Component == NULL) return false;
|
||||
// Must be a default component, or a used additional component.
|
||||
PDEVICE_ENTRY Entry = (PDEVICE_ENTRY)Component;
|
||||
for (ULONG def = 0; def < sizeof(s_DefaultComponents) / sizeof(s_DefaultComponents[0]); def++) {
|
||||
if (Entry == s_DefaultComponents[def]) return ValueForDefault;
|
||||
}
|
||||
|
||||
for (ULONG i = 0; i < g_AdditionalComponentsCount; i++) {
|
||||
if (Entry == &g_AdditionalComponents[i]) {
|
||||
if (Index != NULL) *Index = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool DeviceEntryIsValid(PCONFIGURATION_COMPONENT Component) {
|
||||
return DeviceEntryIsValidImpl(Component, NULL);
|
||||
}
|
||||
|
||||
static bool DeviceEntryIsValidForDelete(PCONFIGURATION_COMPONENT Component, PULONG Index) {
|
||||
return DeviceEntryIsValidImpl(Component, Index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a new component entry as a child of Component.
|
||||
/// </summary>
|
||||
/// <param name="Component">Parent component which will be added to.</param>
|
||||
/// <param name="NewComponent">Child component to add.</param>
|
||||
/// <param name="ConfigurationData">Resource list of the child component.</param>
|
||||
/// <returns></returns>
|
||||
static PCONFIGURATION_COMPONENT ArcAddChild(
|
||||
IN PCONFIGURATION_COMPONENT Component,
|
||||
IN PCONFIGURATION_COMPONENT NewComponent,
|
||||
IN PVOID ConfigurationData OPTIONAL
|
||||
) {
|
||||
// Component must be valid.
|
||||
// Other implementations allow to replace the root component entirely.
|
||||
// We do not.
|
||||
if (Component == NULL) return NULL;
|
||||
if (!DeviceEntryIsValid(Component)) return NULL;
|
||||
|
||||
// Ensure there's enough space to allocate an additional component.
|
||||
if (g_AdditionalComponentsCount >= sizeof(g_AdditionalComponents) / sizeof(g_AdditionalComponents[0])) return NULL;
|
||||
|
||||
ULONG ConfigDataLength = NewComponent->ConfigurationDataLength;
|
||||
if (ConfigurationData == NULL) ConfigDataLength = 0;
|
||||
|
||||
// ...and for the configuration data
|
||||
if (g_ConfigurationDataOffset + ConfigDataLength >= sizeof(g_ConfigurationDataBuffer)) return NULL;
|
||||
|
||||
// Allocate the device entry.
|
||||
PDEVICE_ENTRY Entry = &g_AdditionalComponents[g_AdditionalComponentsCount];
|
||||
g_AdditionalComponentsCount++;
|
||||
|
||||
// Copy the new component to the list.
|
||||
Entry->Component = *NewComponent;
|
||||
|
||||
// If no config data was specified, ensure the length is zero.
|
||||
if (ConfigurationData == NULL) Entry->Component.ConfigurationDataLength = 0;
|
||||
|
||||
// Set the parent.
|
||||
Entry->Parent = (PDEVICE_ENTRY)Component;
|
||||
|
||||
// Copy the configuration data.
|
||||
if (ConfigDataLength != 0) {
|
||||
Entry->ConfigData = (PCM_PARTIAL_RESOURCE_LIST_HEADER)&g_ConfigurationDataBuffer[g_ConfigurationDataOffset];
|
||||
memcpy(Entry->ConfigData, ConfigurationData, ConfigDataLength);
|
||||
g_ConfigurationDataOffset += ConfigDataLength;
|
||||
}
|
||||
|
||||
// Set the new entry as last child of parent.
|
||||
PDEVICE_ENTRY Parent = (PDEVICE_ENTRY)Component;
|
||||
if (Parent->Child == NULL) Parent->Child = Entry;
|
||||
else {
|
||||
PDEVICE_ENTRY This = Parent->Child;
|
||||
for (; This->Peer != NULL; This = This->Peer) {}
|
||||
This->Peer = Entry;
|
||||
}
|
||||
|
||||
// All done.
|
||||
return &Entry->Component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a component entry. Can not delete an entry with children, or an entry that wasn't added by AddChild.
|
||||
/// </summary>
|
||||
/// <param name="Component">Component to delete</param>
|
||||
/// <returns>ARC status code.</returns>
|
||||
static ARC_STATUS ArcDeleteComponent(IN PCONFIGURATION_COMPONENT Component) {
|
||||
ULONG ComponentIndex;
|
||||
if (!DeviceEntryIsValidForDelete(Component, &ComponentIndex)) return _EINVAL;
|
||||
|
||||
PDEVICE_ENTRY Entry = &g_AdditionalComponents[ComponentIndex];
|
||||
if (Entry->Parent == NULL) return _EINVAL;
|
||||
if (Entry->Child != NULL) return _EACCES;
|
||||
|
||||
// Get the parent.
|
||||
PDEVICE_ENTRY Parent = Entry->Parent;
|
||||
|
||||
// Point the child to Component's peer.
|
||||
if (Parent->Child == Entry) Parent->Child = Entry->Peer;
|
||||
else {
|
||||
PDEVICE_ENTRY This = Parent->Child;
|
||||
for (; This->Peer != Entry; This = This->Peer) {}
|
||||
This->Peer = Entry->Peer;
|
||||
}
|
||||
|
||||
// Zero out the parent to remove the entry from the component hierarchy.
|
||||
Entry->Parent = NULL;
|
||||
|
||||
return _ESUCCESS;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the next part of an ARC device path.
|
||||
/// </summary>
|
||||
/// <param name="pPath">Pointer to the part of the string to parse. On function success, pointer is advanced past the part that was parsed.</param>
|
||||
/// <param name="ExpectedType">The device type that is expected to be parsed.</param>
|
||||
/// <param name="Key">On function success, returns the parsed key from the string.</param>
|
||||
/// <returns>True if parsing succeeded, false if it failed.</returns>
|
||||
bool ArcDeviceParse(PCHAR* pPath, CONFIGURATION_TYPE ExpectedType, ULONG* Key) {
|
||||
PCHAR ExpectedString = DeviceTable[ExpectedType];
|
||||
|
||||
// Ensure *pPath == ExpectedString (case-insensitive)
|
||||
PCHAR Path = *pPath;
|
||||
while (*ExpectedString != 0) {
|
||||
CHAR This = *Path | 0x20;
|
||||
//if (This < 'a' || This > 'z') return false;
|
||||
if (This != *ExpectedString) return false;
|
||||
ExpectedString++;
|
||||
Path++;
|
||||
}
|
||||
|
||||
// Next char must be '('
|
||||
if (*Path != '(') return false;
|
||||
Path++;
|
||||
|
||||
// Digits followed by ')'
|
||||
ULONG ParsedKey = 0;
|
||||
while (*Path != ')' && *Path != 0) {
|
||||
CHAR This = *Path;
|
||||
if (This < '0' || This > '9') return false;
|
||||
ParsedKey *= 10;
|
||||
ParsedKey += (This - '0');
|
||||
Path++;
|
||||
}
|
||||
if (*Path != ')') return false;
|
||||
Path++;
|
||||
|
||||
// Success
|
||||
*pPath = Path;
|
||||
*Key = ParsedKey;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For an ARC device, get its ARC path.
|
||||
/// </summary>
|
||||
/// <param name="Component">ARC component</param>
|
||||
/// <param name="Path">Buffer to write path to</param>
|
||||
/// <param name="Length">Length of buffer</param>
|
||||
/// <returns>Length written without null terminator</returns>
|
||||
ULONG ArcDeviceGetPath(PCONFIGURATION_COMPONENT Component, PCHAR Path, ULONG Length) {
|
||||
PDEVICE_ENTRY This = (PDEVICE_ENTRY)Component;
|
||||
|
||||
ULONG Offset = 0;
|
||||
// Recurse for each element.
|
||||
if (This->Parent != NULL && This->Parent != &Root) {
|
||||
Offset = ArcDeviceGetPath(&This->Parent->Component, Path, Length);
|
||||
if (Offset == 0) return 0;
|
||||
}
|
||||
|
||||
if (Offset > Length) return 0;
|
||||
|
||||
int Ret = snprintf(&Path[Offset], Length - Offset, "%s(%u)", DeviceTable[This->Component.Type], This->Component.Key);
|
||||
if (Ret < 0) return 0;
|
||||
if (Ret >= (Length - Offset)) return 0;
|
||||
return Offset + Ret;
|
||||
}
|
||||
|
||||
static bool IsDevice(PDEVICE_ENTRY This, PCHAR* pPath) {
|
||||
// Initialise the key to zero.
|
||||
ULONG Key = 0;
|
||||
// Get the path string parsed in, don't update the pointer on a failure.
|
||||
PCHAR Path = *pPath;
|
||||
|
||||
// Try to parse this part of the device string, by this device entry's type.
|
||||
// If it failed, this isn't the device being looked for.
|
||||
if (!ArcDeviceParse(&Path, This->Component.Type, &Key)) return false;
|
||||
|
||||
// Ensure provided key matches the component's
|
||||
if (Key != This->Component.Key) return false;
|
||||
|
||||
// Success
|
||||
*pPath = Path;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Obtains a component from an ARC path string. Returns the best component that can be found in the hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="PathName">Path name to search</param>
|
||||
/// <returns>Found component</returns>
|
||||
static PCONFIGURATION_COMPONENT ArcGetComponent(IN PCHAR PathName) {
|
||||
// Get the root component.
|
||||
PDEVICE_ENTRY Match = &Root;
|
||||
|
||||
// Keep searching until there are no more entries.
|
||||
PCHAR Pointer = PathName;
|
||||
while (*Pointer != 0) {
|
||||
// Search each child.
|
||||
PDEVICE_ENTRY This;
|
||||
for (This = Match->Child; This != NULL; This = This->Peer) {
|
||||
if (IsDevice(This, &Pointer)) {
|
||||
Match = This;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (This == NULL) break;
|
||||
}
|
||||
|
||||
if (Match == &Root) {
|
||||
// The match that was found is the root.
|
||||
// Only match on that if caller really wanted it.
|
||||
Pointer = PathName;
|
||||
ULONG Key;
|
||||
if (!ArcDeviceParse(&Pointer, ArcSystem, &Key)) return NULL;
|
||||
}
|
||||
return &Match->Component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the child of the current component
|
||||
/// </summary>
|
||||
/// <param name="Component">Component to obtain the child of. If null, obtains the root component.</param>
|
||||
/// <returns>Component</returns>
|
||||
static PCONFIGURATION_COMPONENT ArcGetChild(IN PCONFIGURATION_COMPONENT Component OPTIONAL) {
|
||||
if (Component == NULL) return &Root.Component;
|
||||
if (!DeviceEntryIsValid(Component)) return NULL;
|
||||
PDEVICE_ENTRY Child = ((PDEVICE_ENTRY)Component)->Child;
|
||||
if (Child == NULL) return NULL;
|
||||
return &Child->Component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parent of the current component
|
||||
/// </summary>
|
||||
/// <param name="Component">Component to obtain the parent of.</param>
|
||||
/// <returns>Parent component</returns>
|
||||
static PCONFIGURATION_COMPONENT ArcGetParent(IN PCONFIGURATION_COMPONENT Component) {
|
||||
if (Component == NULL) return NULL;
|
||||
if (!DeviceEntryIsValid(Component)) return NULL;
|
||||
PDEVICE_ENTRY Parent = ((PDEVICE_ENTRY)Component)->Parent;
|
||||
if (Parent == NULL) return NULL;
|
||||
return &Parent->Component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the peer of the current component
|
||||
/// </summary>
|
||||
/// <param name="Component">Component to obtain the peer of.</param>
|
||||
/// <returns>Peer component</returns>
|
||||
static PCONFIGURATION_COMPONENT ArcGetPeer(IN PCONFIGURATION_COMPONENT Component) {
|
||||
if (Component == NULL) return NULL;
|
||||
if (!DeviceEntryIsValid(Component)) return NULL;
|
||||
PDEVICE_ENTRY Peer = ((PDEVICE_ENTRY)Component)->Peer;
|
||||
if (Peer == NULL) return NULL;
|
||||
return &Peer->Component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration data of the current component
|
||||
/// </summary>
|
||||
/// <param name="ConfigurationData">Buffer to write the configuration data into</param>
|
||||
/// <param name="Component">Component to obtain the configuration data of</param>
|
||||
/// <returns>ARC status value</returns>
|
||||
static ARC_STATUS ArcGetData(OUT PVOID ConfigurationData, IN PCONFIGURATION_COMPONENT Component) {
|
||||
if (!DeviceEntryIsValid(Component)) return _EINVAL;
|
||||
|
||||
PDEVICE_ENTRY Device = (PDEVICE_ENTRY)Component;
|
||||
memcpy(ConfigurationData, Device->ConfigData, Device->Component.ConfigurationDataLength);
|
||||
return _ESUCCESS;
|
||||
}
|
||||
|
||||
static ARC_STATUS ArcSaveConfiguration(void) {
|
||||
// No operation.
|
||||
return _ESUCCESS;
|
||||
}
|
||||
|
||||
static ARC_DISPLAY_STATUS s_DisplayStatus = { 0 };
|
||||
static PARC_DISPLAY_STATUS ArcGetDisplayStatus(ULONG FileId) {
|
||||
ArcConsoleGetStatus(&s_DisplayStatus);
|
||||
return &s_DisplayStatus;
|
||||
}
|
||||
|
||||
static ARC_STATUS ArcTestUnicodeCharacter(ULONG FileId, WCHAR UnicodeCharacter) {
|
||||
if ((UnicodeCharacter >= ' ') && (UnicodeCharacter <= '~')) return _ESUCCESS;
|
||||
return _EINVAL;
|
||||
}
|
||||
|
||||
static SYSTEM_ID s_SystemId = { 0 };
|
||||
static char s_VendorId[] = "ArtX";
|
||||
static char s_UnknownProductIdFlipper[] = "Flipper";
|
||||
static char s_UnknownProductIdVegas[] = "Vegas";
|
||||
static char s_UnknownProductIdLatte[] = "Latte";
|
||||
_Static_assert(sizeof(s_VendorId) <= sizeof(s_SystemId.VendorId));
|
||||
static PSYSTEM_ID ArcGetSystemId(void) {
|
||||
return &s_SystemId;
|
||||
}
|
||||
|
||||
#define CONSOLE_WRITE_CONST(str) ArcConsoleWrite((str), sizeof(str) - 1)
|
||||
|
||||
static ULONG s_PrintTreeCount = 0;
|
||||
|
||||
static void ArcPrintDevice(PDEVICE_ENTRY Device) {
|
||||
char ArcPath[1024];
|
||||
if (ArcDeviceGetPath(&Device->Component, ArcPath, sizeof(ArcPath)) == 0) {
|
||||
printf("unknown device at %08x\r\n", Device);
|
||||
}
|
||||
else {
|
||||
printf("%s\r\n", ArcPath);
|
||||
}
|
||||
if (s_PrintTreeCount != 0 && (s_PrintTreeCount & 0x1f) == 0) IOSKBD_ReadChar();
|
||||
s_PrintTreeCount++;
|
||||
}
|
||||
|
||||
static void ArcPrintTreeImpl(PDEVICE_ENTRY Device) {
|
||||
if (Device == NULL) return;
|
||||
|
||||
for (PDEVICE_ENTRY Child = (PDEVICE_ENTRY)ArcGetChild(Device); Child != NULL; Child = (PDEVICE_ENTRY)ArcGetChild(Child)) {
|
||||
ArcPrintDevice(Child);
|
||||
for (PDEVICE_ENTRY This = (PDEVICE_ENTRY)ArcGetPeer(Child); This != NULL; This = (PDEVICE_ENTRY)ArcGetPeer(This)) {
|
||||
ArcPrintDevice(This);
|
||||
if (This->Child != NULL) ArcPrintTreeImpl(This);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ArcPrintTree(PDEVICE_ENTRY Device) {
|
||||
s_PrintTreeCount = 0;
|
||||
ArcPrintTreeImpl(Device);
|
||||
}
|
||||
|
||||
static bool ArcConfigKeyEquals(PDEVICE_ENTRY Lhs, PDEVICE_ENTRY Rhs) {
|
||||
return (
|
||||
(Lhs->Parent == Rhs->Parent) &&
|
||||
(Lhs->Component.Type == Rhs->Component.Type) &&
|
||||
(Lhs->Component.Class == Rhs->Component.Class) &&
|
||||
(Lhs->Component.Key == Rhs->Component.Key)
|
||||
);
|
||||
}
|
||||
|
||||
bool ArcConfigKeyExists(PDEVICE_ENTRY Device) {
|
||||
for (ULONG def = 0; def < sizeof(s_DefaultComponents) / sizeof(s_DefaultComponents[0]); def++) {
|
||||
PDEVICE_ENTRY This = s_DefaultComponents[def];
|
||||
if (This == Device) continue;
|
||||
if (!ArcConfigKeyEquals(Device, This)) continue;
|
||||
return true;
|
||||
}
|
||||
|
||||
for (ULONG i = 0; i < g_AdditionalComponentsCount; i++) {
|
||||
PDEVICE_ENTRY This = &g_AdditionalComponents[i];
|
||||
if (This == Device) continue;
|
||||
if (!ArcConfigKeyEquals(Device, This)) continue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ArcConfigInit(void) {
|
||||
// Initialise vectors.
|
||||
PVENDOR_VECTOR_TABLE Api = ARC_VENDOR_VECTORS();
|
||||
Api->AddChildRoutine = ArcAddChild;
|
||||
Api->DeleteComponentRoutine = ArcDeleteComponent;
|
||||
Api->GetComponentRoutine = ArcGetComponent;
|
||||
Api->GetChildRoutine = ArcGetChild;
|
||||
Api->GetParentRoutine = ArcGetParent;
|
||||
Api->GetPeerRoutine = ArcGetPeer;
|
||||
Api->GetDataRoutine = ArcGetData;
|
||||
Api->SaveConfigurationRoutine = ArcSaveConfiguration;
|
||||
Api->GetDisplayStatusRoutine = ArcGetDisplayStatus;
|
||||
Api->GetSystemIdRoutine = ArcGetSystemId;
|
||||
Api->TestUnicodeCharacterRoutine = ArcTestUnicodeCharacter;
|
||||
|
||||
// Initialise the System ID structure.
|
||||
memset(&s_SystemId, 0, sizeof(s_SystemId));
|
||||
memcpy(&s_SystemId.VendorId, s_VendorId, sizeof(s_VendorId));
|
||||
ARTX_SYSTEM_TYPE SystemType = (ARTX_SYSTEM_TYPE)s_RuntimePointers[RUNTIME_SYSTEM_TYPE].v;
|
||||
switch (SystemType) {
|
||||
case ARTX_SYSTEM_FLIPPER:
|
||||
memcpy(&s_SystemId.ProductId, s_UnknownProductIdFlipper, sizeof(s_UnknownProductIdFlipper));
|
||||
break;
|
||||
case ARTX_SYSTEM_VEGAS:
|
||||
memcpy(&s_SystemId.ProductId, s_UnknownProductIdVegas, sizeof(s_UnknownProductIdVegas));
|
||||
break;
|
||||
case ARTX_SYSTEM_LATTE:
|
||||
memcpy(&s_SystemId.ProductId, s_UnknownProductIdLatte, sizeof(s_UnknownProductIdLatte));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Initialise environment variables.
|
||||
ArcEnvSetVarInMem("CONSOLEIN", KeyboardPath);
|
||||
ArcEnvSetVarInMem("CONSOLEOUT", MonitorPath);
|
||||
|
||||
// Set up the display controller identifier, used by setupldr
|
||||
Vi.Component.Identifier = (size_t)s_DisplayIdentifier;
|
||||
Vi.Component.IdentifierLength = sizeof(s_DisplayIdentifier);
|
||||
|
||||
// Set up the Flipper "bus" identifier, used by NT kernel
|
||||
Flipper.Component.Identifier = (size_t)s_FlipperBusIdentifier;
|
||||
Flipper.Component.IdentifierLength = sizeof(s_FlipperBusIdentifier);
|
||||
|
||||
// Set up the IOS IPC memory range.
|
||||
extern ULONG s_MacIoStart;
|
||||
VegasDesc.Descriptors[2].Memory.Start.LowPart = s_RuntimeIpc.PointerArc;
|
||||
VegasDesc.Descriptors[2].Memory.Length = s_RuntimeIpc.PointerArc;
|
||||
|
||||
|
||||
// Set up the CPU identifier, if this is missing smss will terminate STATUS_OBJECT_NAME_NOT_FOUND
|
||||
// Get the pvr to determine the processor.
|
||||
ULONG Pvr;
|
||||
__asm__ __volatile__ ("mfpvr %0" : "=r"(Pvr));
|
||||
ULONG PvrUpper = Pvr >> 16;
|
||||
ULONG PvrLower = Pvr & 0xFFFF;
|
||||
|
||||
if (PvrUpper == 0x7001) {
|
||||
// Espresso
|
||||
Cpu.Component.Identifier = (size_t)s_EspressoIdentifier;
|
||||
Cpu.Component.IdentifierLength = sizeof(s_EspressoIdentifier);
|
||||
}
|
||||
else if (PvrUpper == 0x7000) {
|
||||
// Gekko prototypes
|
||||
Cpu.Component.Identifier = (size_t)s_GekkoIdentifier;
|
||||
Cpu.Component.IdentifierLength = sizeof(s_GekkoIdentifier);
|
||||
}
|
||||
else if (PvrUpper != 8) {
|
||||
// ??? assume this is gekko
|
||||
Cpu.Component.Identifier = (size_t)s_GekkoIdentifier;
|
||||
Cpu.Component.IdentifierLength = sizeof(s_GekkoIdentifier);
|
||||
}
|
||||
else if (PvrLower >= 0x7000) {
|
||||
// Broadway
|
||||
Cpu.Component.Identifier = (size_t)s_BroadwayIdentifier;
|
||||
Cpu.Component.IdentifierLength = sizeof(s_BroadwayIdentifier);
|
||||
}
|
||||
else {
|
||||
// Gekko
|
||||
Cpu.Component.Identifier = (size_t)s_GekkoIdentifier;
|
||||
Cpu.Component.IdentifierLength = sizeof(s_GekkoIdentifier);
|
||||
}
|
||||
|
||||
// Set up the system / chipset identifier
|
||||
switch (SystemType) {
|
||||
case ARTX_SYSTEM_FLIPPER:
|
||||
Root.Component.Identifier = (size_t)s_RootIdentifierFlipper;
|
||||
Root.Component.IdentifierLength = sizeof(s_RootIdentifierFlipper);
|
||||
break;
|
||||
case ARTX_SYSTEM_VEGAS:
|
||||
Root.Component.Identifier = (size_t)s_RootIdentifierVegas;
|
||||
Root.Component.IdentifierLength = sizeof(s_RootIdentifierVegas);
|
||||
break;
|
||||
case ARTX_SYSTEM_LATTE:
|
||||
Root.Component.Identifier = (size_t)s_RootIdentifierLatte;
|
||||
Root.Component.IdentifierLength = sizeof(s_RootIdentifierLatte);
|
||||
break;
|
||||
}
|
||||
|
||||
// Fix up the device tree for flipper
|
||||
if (SystemType == ARTX_SYSTEM_FLIPPER) {
|
||||
// Remove vegas from the device tree, this also cuts off usb (placed after vegas)
|
||||
// Instead, add the dummy scsi adapter
|
||||
Flipper.Peer = &ScsiDummy;
|
||||
ScsiDummy.Component.Identifier = (size_t)s_ScsiDummyIdentifier;
|
||||
ScsiDummy.Component.IdentifierLength = sizeof(s_ScsiDummyIdentifier);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user