Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Reborn
Browse files Browse the repository at this point in the history
  • Loading branch information
noriyotcp committed Dec 24, 2017
1 parent bb3b26b commit 812e8fe
Show file tree
Hide file tree
Showing 145 changed files with 2,589 additions and 13,224 deletions.
16 changes: 5 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# Compiled files
/build/
*.o

# Executables
*.a
*.bin

# A generated file when builing a Crystal program with --cross-compile option
bc_flags

# Crystal version
.crystal-version

# Generated by crystal build
/target/

# Generated files
*.iso
*.log
*.a

# vscode config
.vscode/
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "src/musl"]
path = src/musl
[submodule "musl"]
path = musl
url = git://git.musl-libc.org/musl
ignore = dirty
2 changes: 1 addition & 1 deletion LICENSE-APACHE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright (c) 2016-2017 Utero OS Project Developers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Utero OS Project Developers
Copyright (c) 2016-2017 Utero OS Project Developers

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
Expand Down
161 changes: 80 additions & 81 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,101 +1,100 @@
# Copyright (c) 2016-2017 Utero OS Developers. See the COPYRIGHT
# file at the top-level directory of this distribution.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
#
# The part of this file was taken from:
# https://github.com/phil-opp/blog_os/blob/set_up_rust/Makefile
EXT_MUSL = $(EXT_DIR)/musl

arch ?= x86_64
target ?= $(arch)-unknown-linux-musl
kernel := build/kernel-$(arch).bin
iso := build/utero-$(arch).iso
top_dir := $(shell pwd)
INCLUDE := -I$(top_dir)/src/c_kernel/include -I$(top_dir)/src/arch/x86_64/c/include
CFLAGS := -O2 -ffreestanding -nostdinc -Wno-implicit
CC = $(EXT_MUSL)/bin/musl-gcc
LD ?= ld
AR ?= ar
NASM ?= nasm

libcr := src/musl/lib/libcr.a
libu := build/arch/$(arch)/c/libu.a
libu_fullpath := $(subst build/,$(shell pwd)/build/,$(libu))
OS_NAME = utero
BUILD_DIR = build
BUILD_DIRS = $(dir $(OBJECTS) $(SOURCES) $(CRYSTAL_SOURCES))
EXT_DIR = ext
LINKER = linker.ld
ISO_DIR = $(BUILD_DIR)/isofiles
KERNEL_DIR = $(ISO_DIR)/boot
GRUB_DIR = $(KERNEL_DIR)/grub
KERNEL = $(KERNEL_DIR)/kernel.bin
ISO = $(BUILD_DIR)/$(OS_NAME).iso
LIB = $(BUILD_DIR)/lib$(OS_NAME).a
CRYSTAL_OS = $(filter %main.o,$(CRYSTAL_SOURCES))

c_source_files := $(wildcard src/arch/$(arch)/c/*.c)
c_object_files := $(patsubst src/arch/$(arch)/c/%.c, \
build/arch/$(arch)/c/%.o, $(c_source_files))
ARCH ?= x86_64
TARGET ?= $(ARCH)-unknown-linux-musl

c_kernel_source_files := $(wildcard src/c_kernel/*.c)
c_kernel_object_files := $(patsubst src/c_kernel/%.c, \
build/c_kernel/%.o, $(c_kernel_source_files))
OBJECTS := $(patsubst %.asm,build/%.o,$(shell find asm -name '*.asm'))
SOURCES := $(patsubst %.c,build/%.o,$(shell find src -name '*.c'))
CRYSTAL_SOURCES := $(patsubst %.cr,build/%.o,$(shell find src -name '*.cr'))

assembly_source_files := $(wildcard src/arch/$(arch)/*.asm)
assembly_object_files := $(patsubst src/arch/$(arch)/%.asm, \
build/arch/$(arch)/%.o, $(assembly_source_files))
crystal_files := $(shell find ./ -name *.cr)
CFLAGS = -W -Wall -pedantic -std=c11 -O2 -ffreestanding -nostdinc \
-fno-builtin -fno-stack-protector \
-mno-red-zone \
-I src/include/ -I ext/

c_object_files_fullpath := $(subst build/,$(shell pwd)/build/,$(c_object_files))
default: iso

OBJS = $(assembly_object_files) $(c_object_files) $(c_kernel_object_files)
kernel: $(KERNEL)
.PHONY: kernel

crystal_os := target/$(target)/debug/main.o
sources:
@echo $(CRYSTAL_SOURCES)
@echo $(CRYSTAL_OS)

linker_script := src/arch/$(arch)/linker.ld
grub_cfg := src/arch/$(arch)/grub.cfg
$(KERNEL): musl build_dirs $(CRYSTAL_OS) $(OBJECTS) $(LIB)
mkdir -p $(KERNEL_DIR)
$(LD) --nmagic --output=$@ --script=$(LINKER) $(CRYSTAL_OS) $(OBJECTS) $(LIB) $(EXT_MUSL)/lib/libc.a

.PHONY: all test clean run iso
build/asm/%.o: asm/%.asm
$(NASM) -f elf64 $< -o $@

all: $(kernel)
build/src/%.o: src/%.c
$(CC) $(CFLAGS) -c $< -o $@

test:
@crystal spec -v
$(CRYSTAL_OS): $(CRYSTAL_SOURCES)
crystal build src/kernel/main.cr --cross-compile --target $(TARGET) --prelude=empty --verbose
mv -f main.o $@

clean:
@rm -f $(kernel) $(iso) $(OBJS) $(libu)
@rm -rf target/
$(MAKE) -C build/musl clean

cleanobjs:
@rm -f $(OBJS)
@rm -rf target/

run: $(iso)
@qemu-system-$(arch) -cdrom $(iso) -monitor stdio
$(LIB): $(SOURCES)
$(AR) rcs $@ $^

iso: $(iso)
build_dirs:
mkdir -p $(BUILD_DIRS)
.PHONY: build_dirs

$(iso): $(kernel) $(grub_cfg)
@mkdir -p build/isofiles/boot/grub
@cp $(kernel) build/isofiles/boot/kernel.bin
@cp $(grub_cfg) build/isofiles/boot/grub
@grub-mkrescue -o $(iso) build/isofiles 2> /dev/null
@rm -r build/isofiles
musl:
$(MAKE) -C $(EXT_DIR)
.PHONY: musl

$(kernel): $(linker_script) $(libcr) $(libu) $(crystal_os)
@echo Creating $@...
@ld -n -nostdlib -melf_$(arch) --gc-sections --build-id=none -T $(linker_script) -o $@ $(crystal_os) $(libu) $(libcr)
iso: $(ISO)
.PHONY: iso

$(crystal_os): $(libu) $(crystal_files)
@mkdir -p $(shell dirname $(crystal_os))
@crystal build src/kernel/main.cr --target=$(target) --prelude=empty --emit=obj --verbose --link-flags $(libu_fullpath)
@rm main
@mv -f main.o target/$(target)/debug/
$(ISO): cleansrcs $(KERNEL)
mkdir -p $(GRUB_DIR)
cp -R grub/* $(GRUB_DIR)
grub-mkrescue -o $@ $(ISO_DIR)

$(libcr):
$(MAKE) -C build/musl
run: $(ISO)
qemu-system-x86_64 -cdrom $<
.PHONY: run

$(libu): $(OBJS)
@ld -n -nostdlib -melf_$(arch) --build-id=none -r -T $(linker_script) -o $@ $(c_kernel_object_files) $(c_object_files) $(assembly_object_files)
debug: CFLAGS += -DENABLE_KERNEL_DEBUG
debug: cleaniso $(ISO)
qemu-system-x86_64 -cdrom $(ISO) -serial file:/tmp/serial.log
.PHONY: debug

build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm
@mkdir -p $(shell dirname $@)
@nasm -felf64 $< -o $@

build/arch/$(arch)/c/%.o: src/arch/$(arch)/c/%.c
@mkdir -p $(shell dirname $@)
@cc $(CFLAGS) $(INCLUDE) -o $@ -c $<

build/c_kernel/%.o: src/c_kernel/%.c
@mkdir -p $(shell dirname $@)
@cc $(CFLAGS) $(INCLUDE) -o $@ -c $<
clean:
rm -f $(OBJECTS) $(SOURCES) $(CRYSTAL_OS) $(KERNEL) $(ISO) $(LIB)
rm -rf $(BUILD_DIR)
.PHONY: clean

cleansrcs:
rm -f $(SOURCES)
.PHONY: cleansrcs

cleaniso:
rm -f $(ISO)
.PHONY: cleaniso

cleanmusl:
rm -rf $(EXT_MUSL)
$(MAKE) -C $(EXT_DIR) clean
.PHONY: cleanmusl
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# utero
![utero screenshot 2017-05-08 13-43](https://cloud.githubusercontent.com/assets/5820754/25790536/5d692c88-33f4-11e7-862d-8eaa602f7e61.gif)
<img src="doc/utero 2017-12-24.gif" alt="utero 2017-12-24">

**Utero** is an operating system (for x86_64) written in [Crystal](https://crystal-lang.org/) *as much as possible*.

Expand All @@ -19,19 +19,57 @@ This is the *work in progress*.

## Usage
To make an ISO file and run on Qemu

``$ make run``

To make an ISO file

``$ make iso``

To make a binary file of the kernel

``$ make`` or ``$ make all``

To compile the OS in DEBUG mode and run on Qemu

``$ make debug``

**Note:** in DEBUG mode, logging uses the serial port `COM1` to write various
debugging information. `qemu` is configured to write the output of this serial
port to `/tmp/serial.log`.

To clean up

``$ make clean``

To test (crystal spec)
``$ make test``
### Troubleshooting on Qemu
On a system that uses EFI boot, like dual boot macOS and Ubuntu(16.04) on MacBook Pro

Error on Qemu like this

```sh
Could not read from CDROM (code 0009)
```

The solution may be:

```sh
$ sudo apt-get install grub-pc-bin
```

After the install `grub-pc-bin`, you will need to recreate the ISO file like this:

```sh
$ make clean
$ make # or make iso
$ make run
```

The following links saved my life:

https://github.com/Microsoft/WSL/issues/1043

http://intermezzos.github.io/book/appendix/troubleshooting.html#Could%20not%20read%20from%20CDROM%20%28code%200009%29

## Contributing

Expand Down
3 changes: 3 additions & 0 deletions REFERENCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ https://github.com/redcap97/cyanurus
### eduOS
https://github.com/RWTH-OS/eduOS

### willOS
https://github.com/willdurand/willOS/tree/f8989809ba93437abe52ee14b0a0623adc0abea1

-----

### Create Your Own Operating System: Build, deploy, and test your very own operating systems for the Internet of Things and other devices Kindle Edition
Expand Down
Loading

0 comments on commit 812e8fe

Please sign in to comment.