==========================
multiboot/Upsilon/KhiCAS for the Prime G1
==========================

1/ Introduction
=========
When the G1 starts, it loads Besta OS, then the OS looks in the NAND flash for some files, these files can be sent to the HP by modifying the PRIME_APP.DAT file in the hp connectivity kit Firmware/PrimeG1 directory. This file can easily be modified, then sent back to the Prime G1 using the HP Connectivity Kit, enabling native devel on the G1.
I have developped a multi-firmware loader, it can currently load HP/Moravia original firmwares or an alternative firmware named Upsilon, derived from the latest Numworks Epsilon OS when it was modifiable and redistribuable. Upsilon can run "external applications", that is native ARM applications written in C/C++. KhiCAS is the first native App for Upsilon, it's a full port of an up to date version of Giac/Xcas for the Prime G1, unlike the CAS version of HP/moravia firmwares that were not updated since about 5 years.
An advantage of KhiCAS is that it's a full GPL port, linked to GMP/MPFR/MPFI with big integer support, multiprecision floats, interval arithmetic. It has an optimized UI for CAS sessions:
* expression editor where you can select part of an expression and apply commands on this part (e.g. factor or expand only a part of an expression)
* syntax highlighting, 
* parenthese match
* program editor with auto-indentation
* sessions and scripts can be saved or loaded from the storage nand flash of the Prime without any software installation, just connect the Prime to the PC, it will be recognized as an USB key, scripts and sessions are in the xcas directory.
N.B.: I also have an HP firmware with an up to date Giac version but I'm still waiting for Moravia authorization to publish it. The multiloader/Upsilon and KhiCAS do not include code from Moravia, therefore I can publish KhiCAS for the G1 independantly.

2/ Installation
=========
[Legal mode on]Note that the software provided here comes without support and without warranty. Moravia, HP, Besta are *not* involved and Numworks is only involved because they let everyone fork and redistribute Epsilon up to version 15.5[Legal mode off]
2.1/ you must modify a G1 HP firmware. You can do that from a web browser
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/appdisk_en.html
2.2/ Copy the modified file in the Firmware/PrimeG1 directory of the connectivity kit (Windows:  in Documents/HP Connectivity Kit/Firmware [translated]/PrimeG1)
2.3/ Hold SYMB key on your G1, press reset, to open Updater mode. Open the connectivity kit on the PC, after a few seconds, the update process will start. Once update is finished press Reset.
2.4/ Now the multiloader will let you choose a firmware, currently supported HP original (Enter), Upsilon (Esc). If you want to check another HP firmware or a modified Upsilon firmware this is also supported (key 4 or 5, see later).
2.5/ If you choose HP original firmware, then you should be back to the normal HP Prime behaviour. Press reset to run Upsilon
2.6/ If you choose Upsilon, you will see the Home screen of the Numworks OS. It is currently not touchscreen-enabled. The Back key of the Numworks is the Esc key on the Prime, other keys should be straightforward to guess.
2.7/ If you press the Vars key from Home screen of Upsilon while the Prime is connected to a PC, the Prime will behave like a USB key exposing it's C: Drive to a drive on your PC (e.g. D: on Windows if you have no other mass storage connected). To install native apps, including KhiCAS, on the Prime, create a directory named apps on the Prime USB key (e.g. from Powershell under Windows with mkdir d:apps).
2.8/ KhiCAS port to the G1 is available from
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/khicas/app.elf
Copy it in the apps directory of the G1, e.g. from Powershell with the command copy app.elf d:\apps
2.9/ To run KhiCAS from Upsilon, press Esc to move to the last app, then Enter.
Unlike Upsilon, KhiCAS has support for the touchscreen.
2.10/ If you want to develop native apps for the G1, I have implemented an SDK
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/g1sdk.zip
Unzip it, install the arm gcc toolchain and run make in hello or eventg1 or ksdk directory, the resulting elf file can be copied to the \apps directory of the C: drive of the Prime. Use the External app (shortcut 9 from Home screen of Upsilon) to launch your native app. Many native games and applications should compile on the Prime!

3/ Note 1: the patch used is completly different from Newrpl. Newrpl does not load besta OS, it provides an alternative PRIME_OS.ROM. It's not clear how easy/hard it's to build and load native apps on this alternative rom.
Note 2: I have not looked at the backdoor used to run native code inside Python on the G1. Since the G1 firmware is an elf file contained in PRIME_APP.DAT, it's certainly possible to modify HP firmware on the G1 to run native code, but you would have to do reverse-engeniring for each version of the firmware.

==================================
SDK for the Prime G1 with arm-none-eabi-gcc toolchain
==================================
The SDK is based on the arm gcc toolchain with Project Muteki reversed engineering syscalls for the Besta OS of the Prime G1. I assume you are running Linux, either natively or in a VM or in WSL under Windows.
You have to install the arm gcc toolchain (gcc-arm-none-eabi  package in Debian and derivatives) and I recommend also udisks2, then
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/g1sdk.zip

When the G1 starts, it loads Besta OS, then the OS looks in the NAND flash for some files, these files can be sent to the HP by modifying the PRIME_APP.DAT file in the hp connectivity kit Firmware/PrimeG1 directory. This file can easily be modified from Linux or WSL under Windows with the following command
sudo mkdir /mnt/appdisk
sudo mount -o loop,offset=8192,rw,user,umask=000 path_to/PRIME_APP.DAT /mnt/appdisk
...
sudo umount /mnt/appdisk
or with udisks commands (see g1.sh and g1app.sh in the hellog1 and eventg1 directory)
Powershell equivalent under Windows without installing WSL
Install OSFMount https://www.osforensics.com/tools/mount-disk-images.html
# activate shellscripts from an admin powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Unblock-File Mount-AppDisk.ps1
Unblock-File Dismount-AppDisk.ps1
# Mount from admin PowerShell
.\Mount-AppDisk.ps1 -DatFile "C:\tmp\APPSDISK.DAT" -DriveLetter X
# modify files on X:
...
# Umount and reinject
.\Dismount-AppDisk.ps1 -DatFile "C:\tmp\APPSDISK.DAT" -DriveLetter X

After boot, the Besta OS looks in this system disk (DOS path a:) for armfir.elf and will launch it, this is normally HP firmware [HP firmware loads additional data from armfir.dat] but we can easily replace armfir.elf, I will call armfir.elf the bootfirmware. HP firmware is an elf file located at 0x30600000 (as confirmed by readelf -a armfir.elf to see it)
The G1 RAM memory area is 0x30000000-0x32000000. Some parts are reserved for besta OS: 0x30000000-0x30100000 is for the OS code, 0x31D00000-0x3200000 (near the end) for OS buffers (USB, framebuffer, tables, ...) and probably most memory below 0x30600000 (but I have observed that malloc can return small allocations at addresses below 0x30600000)
We will replace the bootfirmware armfir.elf with a small firmware chooser that can launch the HP firmware or another firmware, Upsilon here.

Current status of the SDK:
* firmware chooser: OK. NEW: the c: filesystem of the Prime can be mounted on a PC and you can exchange files easily with a file manager. The next step will be to move external app support from a: to c:, that way they will be full independant from HP after first install. It's also possible to boot from hp.elf or upsilon.elf in c:\boot. It means you can now test a beta HP firmware version while keeping the current stable version and switch easily after a reset.
* Upsilon alternative firmware: OK with minimal hardware support at this time (only display/key input are supported).
* Upsilon External app support: the "Built-in" app of Upsilon will try to run an app.elf application from path a:\programs\misc (/mnt/appdisk/programs/misc if the APPSDISK.DAT is mounted in Linux). The app.elf must be compiled at a free location inside the memory area end of Upsilon firmware 0x30900000 to 0x31000000. The heap for app.elf is starting at 3100xxxx (after the firmware chooser end) or at the end of app.elf.
* KhiCAS external app: planned but not available right now.
The firmware chooser is available here
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/armfir.elf
Upsilon firmware for the G1 is available here:
https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/upsilon.elf
Upsilon source code https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/upsilon_calc.tar.bz2

Resident external app should abe investigated with a fixedreduced heap reserved by the firmware malloc. This would enable calling Xcas commands from Upsilon (using const char * giac::caseval(const char *))

==========
Testing on a G1:
==========
* Get the file PRIME_APP.DAT from HP Connectivity Kit Firmware/G1 directory (on Windows, in Documents/HP Connectivity Kit/Firmware [translated]/PrimeG1, 
* make a backup copy,
* copy PRIME_APP.DAT to  APPSDISK.DAT somewhere in your Linux path,
* 1st install:
wget https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/armfir.elf
wget https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/upsilon.elf
wget https://www-fourier.univ-grenoble-alpes.fr/~parisse/hpg1/app.elf
sudo mkdir /mnt/appdisk
sudo mount -o loop,offset=8192,rw,user,umask=000 path_to/PRIME_APP.DAT /mnt/appdisk
mv /mnt/appdisk/programs/misc/armfir.elf /mnt/appdisk/programs/misc/hp.elf
cp upsilon.elf armfir.elf app.elf /mnt/appdisk/programs/misc
sudo umount /mnt/appdisk
* if you want to build native Upsilon exernal apps update g1.sh and g1app.sh according to your path where APPDISK.DAT 
* update calculator from HP connectivity kit : press ON-SYMB on the calculator, wait a few seconds and confirm update
* You can restore PRIME_APP.DAT from your backup (press ON-SYMB on the calculator to reflash HP original firmware)

hellog1/eventg1 directory example:
Build as an external app for Upsilon with
   make
g1app.sh is called by make and should reflect your path to APPDISK.DAT and PRIME_APP.DAT, it should copy the modified APPSDISK.DAT to the Firmware/G1 directory of the HP Connectivity Kit

include/muteki.h, include/muteki, include/osdep: muteki headers
  Add -DMUTEKI_HAS_PRIME_UI_EVENT in your compile flags for the right event structure selection on the Prime
lib/hpg1.ld: loader file, should work for other projects
lib/libsyscalls.a: the compiled SVC table, you should link with -lsyscalls to have access to muteki declarations
src/besta_fixes.cc and include/besta_fixes.h: stubs for newlib, and c++ initialization code __cpp_initialize__aeabi_

Ressources used:
==========
mutekishims.tgz source code for the syscalls from Muteki/mutekishims project (Besta OS) -> ./mk -> syscalls.s and libsyscalls.a
muteki-shims was integrated in Project-Muteki, muteki, the syscall list is in https://github.com/Project-Muteki/muteki/tree/master/syscall_def and the include file is muteki.h https://github.com/Project-Muteki/muteki/blob/master/include/muteki.h

Besta OS quirks (on the Prime at least)
========================
Printf will crash after OFF/ON if a scroll is required unless you printf a \f (form feed)
SetFontType crashes, probably fonts are not available
