Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Created June 9, 2024 05:52
Show Gist options
  • Save lupyuen/a5f02da807fe855b1944e567e7ed1473 to your computer and use it in GitHub Desktop.
Save lupyuen/a5f02da807fe855b1944e567e7ed1473 to your computer and use it in GitHub Desktop.
Build NuttX for Ox64 on macOS
#!/usr/bin/env bash
# Build NuttX for Ox64 on macOS
## TODO: Set PATH
export PATH="$HOME/xpack-riscv-none-elf-gcc-13.2.0-2/bin:$PATH"
rm init.S
rm initrd
rm -r boards/risc-v/bl808/ox64/src/etctmp
set -e # Exit when any command fails
set -x # Echo commands
## Build NuttX
function build_nuttx {
## Go to NuttX Folder
pushd ../nuttx
## Build NuttX
make -j 8
## Return to previous folder
popd
}
## Build Apps Filesystem
function build_apps {
## Go to NuttX Folder
pushd ../nuttx
## Build Apps Filesystem
make -j 8 export
pushd ../apps
./tools/mkimport.sh -z -x ../nuttx/nuttx-export-*.tar.gz
make -j 8 import
popd
## Return to previous folder
popd
}
## Pull updates
git pull && git status && hash1=`git rev-parse HEAD`
pushd ../apps
git pull && git status && hash2=`git rev-parse HEAD`
popd
echo NuttX Source: https://github.com/apache/nuttx/tree/$hash1 >nuttx.hash
echo NuttX Apps: https://github.com/apache/nuttx-apps/tree/$hash2 >>nuttx.hash
## Show the versions of GCC and Zig
riscv-none-elf-gcc -v
## Configure build
tools/configure.sh ox64:nsh || true
## Build NuttX
build_nuttx
#### Begin Rust
## Build Apps Filesystem
build_apps || true
## Assume the Current Folder is NuttX Apps Folder.
## Add the Rust Target for 64-bit RISC-V Hard-Float
rustup target add riscv64gc-unknown-none-elf
pushd ../apps/examples/hello_rust
## `$hello` becomes `hello_main.c...apps.examples.hello.o`
## `$hello_rust` becomes `hello_rust_main.rs...apps.examples.hello_rust.o`
hello=$(basename ../hello/*hello.o)
hello_rust=`
echo $hello \
| sed "s/hello_main.c/hello_rust_main.rs/" \
| sed "s/hello.o/hello_rust.o/"
`
## Compile our Rust App for 64-bit RISC-V Hard-Float
rustc \
--target riscv64gc-unknown-none-elf \
--edition 2021 \
--emit obj \
-g \
-C panic=abort \
-O \
hello_rust_main.rs \
-o $hello_rust
## Return to NuttX Apps Folder and build the NuttX Apps
popd
## make import
#### End Rust
## Build Apps Filesystem
build_apps
## Add a.out to Apps Filesystem for TCC
rm -f /tmp/pattern.txt
start=$((0x22056900))
set +x # Don't echo commands
for i in {0..1023}
do
printf 0x%x\\n $(($start + $i)) >> /tmp/pattern.txt
done
set -x # Echo commands
cat /tmp/pattern.txt \
| xxd -revert -plain \
>../apps/bin/a.out
## Generate Initial RAM Disk
genromfs -f initrd -d ../apps/bin -V "NuttXBootVol"
## Show the size
riscv-none-elf-size nuttx
## Export the Binary Image to `nuttx.bin`
riscv-none-elf-objcopy \
-O binary \
nuttx \
nuttx.bin
## Prepare a Padding with 64 KB of zeroes
head -c 65536 /dev/zero >/tmp/nuttx.pad
## Append Padding and Initial RAM Disk to NuttX Kernel
cat nuttx.bin /tmp/nuttx.pad initrd \
>Image
## Copy the config
cp .config nuttx.config
## Dump the disassembly to nuttx.S
riscv-none-elf-objdump \
--syms --source --reloc --demangle --line-numbers --wide \
--debugging \
nuttx \
>nuttx.S \
2>&1
## Run Ox64 TinyEMU
cp $HOME/riscv/nuttx-tinyemu/docs/quickjs/root-riscv64.cfg .
$HOME/riscv/ox64-tinyemu/temu root-riscv64.cfg
rm root-riscv64.cfg
## Exit here if we are running Ox64 Emulator
exit
## Otherwise copy to Ox64 MicroSD...
## Dump the init disassembly to init.S
riscv-none-elf-objdump \
--syms --source --reloc --demangle --line-numbers --wide \
--debugging \
../apps/bin/init \
>init.S \
2>&1
## Dump the hello disassembly to hello.S
riscv-none-elf-objdump \
--syms --source --reloc --demangle --line-numbers --wide \
--debugging \
../apps/bin/hello \
>hello.S \
2>&1
echo ----- Wait for microSD
set +x # Don't echo commands
echo "***** Insert microSD into computer"
while : ; do
if [ -d "/Volumes/NO NAME" ]
then
break
fi
sleep 1
done
sleep 1
set -x # Echo commands
echo ----- Copy to microSD
cp Image "/Volumes/NO NAME/"
ls -l "/Volumes/NO NAME/Image"
## TODO: Verify that /dev/disk5 is microSD
echo ----- Unmount microSD
diskutil unmountDisk /dev/disk5
## Run the firmware
open -a CoolTerm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment