Skip to content

Commit

Permalink
Merge branch 'alloy-update'
Browse files Browse the repository at this point in the history
  • Loading branch information
tay10r committed Apr 20, 2018
2 parents 89130fc + 94d6a21 commit 65e1f4e
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
.DS_Store
/build
/output
/src/AlloyLoader/alloy-loader
*.o
*.bin
*.elf
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
path = src/kernel
url = https://github.com/ReturnInfinity/BareMetal-kernel
[submodule "src/Coreutils"]
path = src/Coreutils
url = https://github.com/ReturnInfinity/Coreutils
path = src/Examples
url = https://github.com/ReturnInfinity/BareMetal-examples
[submodule "src/Pure64"]
path = src/Pure64
url = https://github.com/ReturnInfinity/Pure64
[submodule "src/BMFS"]
path = src/BMFS
url = https://github.com/ReturnInfinity/BMFS
[submodule "src/BareMetal-newlib"]
path = src/BareMetal-newlib
path = src/newlib
url = https://github.com/ReturnInfinity/BareMetal-newlib
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ PREFIX := $(PWD)/output
endif
export PREFIX

C_INCLUDE_PATH=$(PREFIX)/include

export C_INCLUDE_PATH

directories += $(PREFIX)/apps
directories += $(PREFIX)/bin
directories += $(PREFIX)/include
Expand All @@ -23,23 +27,23 @@ systemfiles += $(PREFIX)/system/kernel.sys
systemfiles += $(PREFIX)/system/loader.bin
systemfiles += $(PREFIX)/system/alloy.bin

fs_offset = 32KiB

.PHONY: all
all: $(directories) $(PREFIX)/baremetal-os.img

$(PREFIX)/baremetal-os.img: $(PREFIX)/bin/bmfs $(systemfiles)
$(PREFIX)/bin/bmfs $@ initialize 128M $(PREFIX)/system/bootsectors/bmfs_mbr.sys $(PREFIX)/system/pure64.sys $(PREFIX)/system/kernel.sys $(PREFIX)/system/loader.bin
$(PREFIX)/bin/bmfs $@ mkdir programs
$(PREFIX)/bin/bmfs $@ create alloy.bin 2M
$(PREFIX)/bin/bmfs $@ write alloy.bin $(PREFIX)/system/alloy.bin
$(PREFIX)/bin/bmfs --disk $@ --offset $(fs_offset) format -f
$(PREFIX)/bin/bmfs --disk $@ --offset $(fs_offset) mkdir /System /Applications /Home
$(PREFIX)/bin/bmfs --disk $@ --offset $(fs_offset) cp $(PREFIX)/system/alloy.bin /System/alloy.bin

$(PREFIX)/bin/bmfs $(PREFIX)/lib/libbmfs.a:
$(MAKE) -C src/BMFS install

$(systemfiles):
$(MAKE) -C src/Pure64 install
$(MAKE) -C src/kernel install
$(MAKE) -C src/ironlib install
$(MAKE) -C src/Alloy install
$(MAKE) -C src/Alloy CONFIG=baremetal install

$(directories):
mkdir -p $@
Expand All @@ -49,15 +53,13 @@ clean:
$(MAKE) -C src/Pure64 clean
$(MAKE) -C src/kernel clean
$(MAKE) -C src/BMFS clean
$(MAKE) -C src/ironlib clean
$(MAKE) -C src/Alloy clean
$(MAKE) -C src/Coreutils clean
$(MAKE) -C src/Alloy CONFIG=baremetal clean
$(MAKE) -C src/Examples clean

%.app: $(PREFIX)/apps
$(MAKE) -C src/Coreutils $@
cp --update src/Coreutils/$@ $(PREFIX)/apps/$@
$(PREFIX)/bin/bmfs $(PREFIX)/baremetal-os.img delete $@
$(PREFIX)/bin/bmfs $(PREFIX)/baremetal-os.img create $@ 2M
$(PREFIX)/bin/bmfs $(PREFIX)/baremetal-os.img write $@ src/Coreutils/$@
$(MAKE) -C src/Examples $@
cp --update src/Examples/$@ $(PREFIX)/apps/$@
$(PREFIX)/bin/bmfs --disk $(PREFIX)/baremetal-os.img --offset $(fs_offset) rm -f $@
$(PREFIX)/bin/bmfs --disk $(PREFIX)/baremetal-os.img --offset $(fs_offset) cp $< /Applications/$@

$(V).SILENT:
28 changes: 14 additions & 14 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,16 @@ With GNU Make:

Manual:

cd src/Coreutils/
nasm sysinfo.asm -o ../../bin/sysinfo.app
cd ../../bin
./bmfs bmfs.image create sysinfo.app 2
./bmfs bmfs.image write sysinfo.app
cd src/Examples/
nasm sysinfo.asm -o ../../output/apps/sysinfo.app
cd ../../output
./bmfs --disk baremetal-os.img --offset 32KiB cp apps/sysinfo.app /Applications/sysinfo.app
cd ..
./run.sh

BareMetal OS should be running in the QEMU virtual machine and you should see a '>' prompt. You can now run the application by typing

sysinfo.app
Applications/sysinfo.app


Programs in C
Expand All @@ -119,17 +118,18 @@ With GNU Make:

Manual (will not work with standard C library):

gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -o bin/program.o program.c
ld -T src/Coreutils/coreutil.ld -o bin/program.app bin/program.o
cd bin
./bmfs bmfs.image create program.app 2
./bmfs bmfs.image write program.app
cd ..
cd src/Examples
gcc -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -mcmodel=large -c program.c -o program.o
gcc -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -mcmodel=large -c libBareMetal.c -o libBareMetal.o
ld -T example.ld libBareMetal.o program.o -o program
objcopy -O binary program program.app
cd ../..
./bmfs --disk output/baremetal-os.img --offset 32KiB cp program.app /Applications/program.app
./run.sh

BareMetal OS should be running in the QEMU virtual machine and you should see a '>' prompt. You can now run the application by typing

program.app
Applications/program.app


Compiling Newlib
Expand All @@ -153,4 +153,4 @@ The test application can also be built manually:

BareMetal OS should be running in the QEMU virtual machine and you should see a '>' prompt. You can now run the application by typing

test.app
Applications/test.app
30 changes: 20 additions & 10 deletions app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ export OUTPUT_DIR="$PWD/output"
CC=gcc
CFLAGS=""
CFLAGS+=" -Wall -Wextra -Werror -Wfatal-errors -std=gnu99"
CFLAGS+=" -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -fpie"
CFLAGS+=" -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -mcmodel=large"
CFLAGS+=" -I$OUTPUT_DIR/include"
CFLAGS+=" -Wl,-T src/Coreutils/coreutil.ld"
LIBS="$OUTPUT_DIR/lib/libc.a $OUTPUT_DIR/lib/libbmfs.a"

LD=ld
LDFLAGS="-T src/Examples/example.ld"

OBJCOPY=objcopy

# if no filename is passed, list files
if [[ $# -eq 0 ]]; then
echo "Enter a filename to compile"
echo
echo "Available options:"
echo "$(ls "$PWD/src/Coreutils" | grep '\.asm')"
echo "$(ls "$PWD/src/Coreutils" | grep '\.c')"
echo "$(ls "$PWD/src/Examples" | grep '\.asm')"
echo "$(ls "$PWD/src/Examples" | grep '\.c')"
exit 0
fi

Expand All @@ -36,14 +38,22 @@ mkdir -p "$OUTPUT_DIR/apps"

# compile 'em!
if [[ "$ext" == "asm" ]]; then
nasm -Isrc/Coreutils/ src/Coreutils/$fname.asm -o "$OUTPUT_DIR/apps/$fname.app"
nasm -Isrc/Examples/ src/Examples/$fname.asm -o "$OUTPUT_DIR/apps/$fname.app"
elif [[ "$ext" == "c" ]]; then
$CC $CFLAGS src/Coreutils/$fname.c -o "$OUTPUT_DIR/apps/$fname.app" $LIBS
$CC $CFLAGS -c src/Examples/libBareMetal.c -o src/Examples/libBareMetal.o
$CC $CFLAGS -c src/Examples/$fname.c -o src/Examples/$fname.o
$LD $LDFLAGS src/Examples/libBareMetal.o src/Examples/$fname.o -o "$OUTPUT_DIR/apps/$fname"
$OBJCOPY -O binary "$OUTPUT_DIR/apps/$fname" "$OUTPUT_DIR/apps/$fname.app"
elif [[ "$ext" == "" ]]; then
echo "No file extension found."
exit 1
else
echo "Unknown file extension '$ext'"
exit 1
fi

# put in filesystem
cd "$OUTPUT_DIR"
bin/bmfs baremetal-os.img delete $fname.app
bin/bmfs baremetal-os.img create $fname.app 2
bin/bmfs baremetal-os.img write $fname.app "$OUTPUT_DIR/apps/$fname.app"
bin/bmfs --disk baremetal-os.img --offset 32KiB rm -f "/Applications/$fname.app"
bin/bmfs --disk baremetal-os.img --offset 32KiB cp "$OUTPUT_DIR/apps/$fname.app" "/Applications/$fname.app"
cd ..
11 changes: 8 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
set -u

export OUTPUT_DIR="$PWD/output"
export C_INCLUDE_PATH="$PWD/output/include"

export ALLOY_WITH_BAREMETAL=1

Expand All @@ -23,14 +24,18 @@ cd BMFS
cd ..

cd ironlib
#./build.sh
#./install.sh
cd ..

cd AlloyLoader
./build.sh
./install.sh
cp --update alloy-loader.bin "$OUTPUT_DIR/system/loader.bin"
cd ..

cd Alloy
./build.sh
mv src/loader.bin "$OUTPUT_DIR/system"
mv src/alloy.elf "$OUTPUT_DIR/system"
mv src/alloy "$OUTPUT_DIR/system"
mv src/alloy.bin "$OUTPUT_DIR/system"
cd ..

Expand Down
4 changes: 2 additions & 2 deletions format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -e
export OUTPUT_DIR="$PWD/output"

cd "$OUTPUT_DIR"
bin/bmfs baremetal-os.img initialize 128M
bin/bmfs baremetal-os.img mkdir programs
bin/bmfs --offset 32KiB --disk baremetal-os.img format --force --size 128M
bin/bmfs --offset 32KiB --disk baremetal-os.img mkdir /System /Applications /Home
5 changes: 2 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ cat "$OUTPUT_DIR/system/pure64.sys" \
dd if="$OUTPUT_DIR/system/bmfs_mbr.sys" of="$OUTPUT_DIR/baremetal-os.img" conv=notrunc
dd if="$OUTPUT_DIR/system/software.sys" of="$OUTPUT_DIR/baremetal-os.img" bs=512 seek=16 conv=notrunc
echo Writing Alloy.bin
bin/bmfs baremetal-os.img delete alloy.bin
bin/bmfs baremetal-os.img create alloy.bin 2M
bin/bmfs baremetal-os.img write alloy.bin system/alloy.bin
bin/bmfs --offset 32KiB --disk baremetal-os.img rm -f /System/alloy.bin
bin/bmfs --offset 32KiB --disk baremetal-os.img cp system/alloy.bin /System/alloy.bin
2 changes: 1 addition & 1 deletion src/Alloy
Submodule Alloy updated from 932980 to b2d52f
132 changes: 132 additions & 0 deletions src/AlloyLoader/alloy-loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <bmfs/bmfs.h>

#include "syscalls.h"

extern unsigned char sector[4096];

static bmfs_uint64 round_to_block(bmfs_uint64 size);

void _start(void)
{
/* This is the sector that the file
* system begins at. */
const bmfs_uint64 fs_offset = 8;

/* Number of arguments passed to Alloy. */
int argc = 1;

/* The arguments passed to Alloy. */
const char *argv[] = {
"alloy",
0
};

/* Where Alloy will be loaded. */
void (*alloy_entry)(int, const char **argv) = (void(*)(int, const char **)) 0x200000;

/* Read the file system header */
b_disk_read(sector, fs_offset, 1, 0);
if ((sector[0] != 'B')
|| (sector[1] != 'M')
|| (sector[2] != 'F')
|| (sector[3] != 'S'))
{
b_output("Not a BMFS formatted drive.\n");
return;
}

struct BMFSEntry *root_entry = (struct BMFSEntry *) &sector[512];

bmfs_uint64 dir_offset = root_entry->Offset;

unsigned long int read_count = b_disk_read(sector, fs_offset + (dir_offset / 4096), 1, 0);
if (read_count != 1)
{
b_output("Failed to read '/'.\n");
return;
}

/* Look for the '/sbin' directory. */

struct BMFSEntry *sbin_ent = BMFS_NULL;

for (bmfs_uint64 i = 0; i < 4096; i += 256)
{
struct BMFSEntry *ent = (struct BMFSEntry *) &sector[i];
if ((ent->Name[0] == 'S')
&& (ent->Name[1] == 'y')
&& (ent->Name[2] == 's')
&& (ent->Name[3] == 't')
&& (ent->Name[4] == 'e')
&& (ent->Name[5] == 'm')
&& (ent->Name[6] == 0))
{
b_output("Found '/System'.\n");
sbin_ent = ent;
break;
}
}

if (sbin_ent == BMFS_NULL)
{
b_output("Failed to find '/System'.\n");
return;
}

bmfs_uint64 sbin_offset = sbin_ent->Offset;

read_count = b_disk_read(sector, fs_offset + (sbin_offset / 4096), 1, 0);
if (read_count != 1)
{
b_output("Failed to read '/System'.\n");
return;
}

struct BMFSEntry *alloy_ent = BMFS_NULL;

for (bmfs_uint64 i = 0; i < 4096; i += 256)
{
struct BMFSEntry *ent = (struct BMFSEntry *) &sector[i];
if ((ent->Name[0] == 'a')
&& (ent->Name[1] == 'l')
&& (ent->Name[2] == 'l')
&& (ent->Name[3] == 'o')
&& (ent->Name[4] == 'y')
&& (ent->Name[5] == '.')
&& (ent->Name[6] == 'b')
&& (ent->Name[7] == 'i')
&& (ent->Name[8] == 'n')
&& (ent->Name[9] == 0))
{
b_output("Found '/System/alloy.bin'.\n");
alloy_ent = ent;
break;
}
}

if (alloy_ent == BMFS_NULL)
{
b_output("Failed to find '/System/alloy.bin'.\n");
return;
}

bmfs_uint64 alloy_sector = alloy_ent->Offset;

bmfs_uint64 alloy_size = alloy_ent->Size;

read_count = b_disk_read(alloy_entry, fs_offset + (alloy_sector / 4096), round_to_block(alloy_size), 0);
if (read_count != round_to_block(alloy_size))
{
b_output("Failed to read '/System/alloy.bin'.\n");
return;
}

alloy_entry(argc, argv);
}

static bmfs_uint64 round_to_block(bmfs_uint64 size)
{
return ((size + (4095)) / 4096) * 4096;
}

unsigned char sector[4096];
Loading

0 comments on commit 65e1f4e

Please sign in to comment.