├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── limine.cfg ├── link.ld ├── makefile ├── scripts └── make-image.sh └── src ├── bootstrap.cpp └── stivale2.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | *.iso 3 | limine.h 4 | *.elf 5 | *.o 6 | *.d 7 | limine 8 | *.sb3 9 | src/output.cpp 10 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "thirdparty/limine"] 2 | path = thirdparty/limine 3 | url = https://github.com/limine-bootloader/limine 4 | branch = v3.0-branch-binary 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Scratch Native 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ScratchOS 2 | This is a basic operating system written in scratch3, writing to the vga buffer. 3 | 4 | 1. You need to have `scratchnative` and `scratch2exe.py` in your `PATH`, as well as a gcc x86_64 cross compiler 5 | 2. Run `make run` 6 | 3. Enjoy! 7 | -------------------------------------------------------------------------------- /limine.cfg: -------------------------------------------------------------------------------- 1 | # Timeout in seconds that Limine will use before automatically booting. 2 | TIMEOUT=3 3 | 4 | GRAPHICS=no 5 | 6 | # The entry name that will be displayed in the boot menu 7 | :scratchOS 8 | 9 | # Change the protocol line depending on the used protocol. 10 | PROTOCOL=stivale2 11 | 12 | # Path to the kernel to boot. boot:/// represents the partition on which limine.cfg is located. 13 | KERNEL_PATH=boot:///kernel.elf 14 | -------------------------------------------------------------------------------- /link.ld: -------------------------------------------------------------------------------- 1 | ENTRY(_start) 2 | 3 | SECTIONS 4 | { 5 | . = 0xffffffff80200000; /* 2 MB in the higher half */ 6 | .stivale2hdr : 7 | { 8 | KEEP(*(.stivale2hdr)) 9 | } 10 | 11 | .text : 12 | { 13 | KEEP(*(.text*)) 14 | } 15 | 16 | .rodata : 17 | { 18 | KEEP(*(.rodata*)) 19 | } 20 | 21 | .data : 22 | { 23 | KEEP(*(.data*)) 24 | } 25 | 26 | .bss : 27 | { 28 | KEEP(*(COMMON)) 29 | KEEP(*(.bss*)) 30 | } 31 | .text : { *(.text) } 32 | .data : { *(.data) } 33 | .bss : { *(.bss) } 34 | end = .; 35 | } 36 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | CXX = x86_64-elf-g++ 2 | LD = x86_64-elf-ld 3 | AS = nasm 4 | 5 | TARGET = $(BUILD_DIRECTORY)/kernel.elf 6 | ISO = scratchOS.iso 7 | 8 | KERNEL_ARGS := -O0 -g -Isrc -Wall -Wextra -Werror -Wno-stringop-overflow -MMD -O2 -std=gnu++20 \ 9 | -ffreestanding \ 10 | -fno-rtti \ 11 | -fno-builtin \ 12 | -fno-exceptions \ 13 | -fno-stack-protector \ 14 | -mno-80387 \ 15 | -nostdlib \ 16 | -mno-mmx \ 17 | -mno-3dnow \ 18 | -mno-sse \ 19 | -mno-sse2 \ 20 | -mno-red-zone \ 21 | -mcmodel=kernel \ 22 | -fno-use-cxa-atexit 23 | 24 | LDFLAGS := \ 25 | -nostdlib \ 26 | -static \ 27 | -z max-page-size=0x1000 \ 28 | -T link.ld 29 | 30 | NASMFLAGS := -felf64 31 | 32 | CPPFILES := src/output.cpp src/bootstrap.cpp 33 | 34 | CPP_OBJ = $(patsubst %.cpp, $(BUILD_DIRECTORY)/%.cpp.o, $(CPPFILES)) 35 | 36 | OBJ = $(CPP_OBJ) 37 | 38 | DEPS = $(patsubst $(BUILD_DIRECTORY)/%.cpp.o, $(BUILD_DIRECTORY)/%.cpp.d, $(CPP_OBJ)) 39 | 40 | 41 | BUILD_DIRECTORY := build 42 | DIRECTORY_GUARD = @mkdir -p $(@D) 43 | 44 | .DEFAULT_GOAL = $(ISO) 45 | 46 | limine: 47 | git clone https://github.com/limine-bootloader/limine.git --branch=v3.0-branch-binary --depth=1 48 | make -C limine 49 | 50 | $(ISO): $(TARGET) limine 51 | @$(SHELL) scripts/make-image.sh > /dev/null 52 | @echo "SH scripts/make-image.sh" 53 | 54 | -include $(DEPS) 55 | 56 | src/output.cpp: 57 | scratch2exe.py fetch 706910275 58 | scratchnative project.json.sb3 -o $@ --freestanding 59 | rm -rf project.json.sb3 60 | 61 | src/limine.h: 62 | curl https://raw.githubusercontent.com/limine-bootloader/limine/trunk/limine.h -o $@ 63 | 64 | $(BUILD_DIRECTORY)/%.cpp.o: %.cpp src/limine.h 65 | @$(DIRECTORY_GUARD) 66 | @$(CXX) $(KERNEL_ARGS) -c -o $@ $< 67 | @echo "CXX " $< 68 | 69 | 70 | $(TARGET): $(OBJ) 71 | @$(LD) $(LDFLAGS) $^ -o $@ 72 | @echo "LD " $@ 73 | 74 | run: $(ISO) 75 | qemu-system-x86_64 -cdrom $< -enable-kvm -serial stdio -rtc base=localtime -m 2G -no-shutdown -no-reboot -cpu host 76 | 77 | .PHONY: clean 78 | clean: 79 | rm -rf $(BUILD_DIRECTORY) $(TARGET) $(ISO) $(MODULE_OBJS) $(TEST_EXE) 80 | -------------------------------------------------------------------------------- /scripts/make-image.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | path="$(dirname $0)/../" 4 | mkdir -p tmp 5 | 6 | cp $path/limine/limine.sys ./tmp/ 7 | cp $path/limine/*.bin ./tmp 8 | cp $path/limine.cfg ./tmp 9 | cp $path/build/kernel.elf ./tmp/kernel.elf 10 | 11 | xorriso -as mkisofs -b limine-cd.bin \ 12 | -no-emul-boot -boot-load-size 4 -boot-info-table \ 13 | --efi-boot limine-cd-efi.bin \ 14 | -efi-boot-part --efi-boot-image --protective-msdos-label \ 15 | ./tmp -o scratchOS.iso 16 | $path/limine/limine-deploy scratchOS.iso 17 | rm -rf ./tmp 18 | -------------------------------------------------------------------------------- /src/bootstrap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static uint8_t stack[0x4000]; 6 | 7 | [[gnu::section(".stivale2hdr"), 8 | gnu::used]] static stivale2_header stivale_hdr = {.entry_point = 0, 9 | .stack = (uintptr_t)stack + 10 | sizeof(stack), 11 | .flags = (1 << 1), 12 | .tags = (uintptr_t)0}; 13 | 14 | static void done(void) { 15 | for (;;) { 16 | __asm__("hlt"); 17 | } 18 | } 19 | 20 | void scratch_main(void); 21 | 22 | // The following will be our kernel's entry point. 23 | extern "C" void _start(void) { 24 | scratch_main(); 25 | 26 | // We're done, just hang... 27 | done(); 28 | } 29 | -------------------------------------------------------------------------------- /src/stivale2.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __STIVALE__STIVALE2_H__ 2 | #define __STIVALE__STIVALE2_H__ 3 | 4 | #include 5 | 6 | #if (defined(_STIVALE2_SPLIT_64) && defined(__i386__)) || \ 7 | defined(_STIVALE2_SPLIT_64_FORCE) 8 | 9 | #define _stivale2_split64(NAME) \ 10 | union { \ 11 | uint32_t NAME; \ 12 | uint32_t NAME##_lo; \ 13 | }; \ 14 | uint32_t NAME##_hi 15 | 16 | #else 17 | 18 | #define _stivale2_split64(NAME) uint64_t NAME 19 | 20 | #endif 21 | 22 | // Anchor for non ELF kernels 23 | struct stivale2_anchor { 24 | uint8_t anchor[15]; 25 | uint8_t bits; 26 | _stivale2_split64(phys_load_addr); 27 | _stivale2_split64(phys_bss_start); 28 | _stivale2_split64(phys_bss_end); 29 | _stivale2_split64(phys_stivale2hdr); 30 | }; 31 | 32 | struct stivale2_tag { 33 | uint64_t identifier; 34 | _stivale2_split64(next); 35 | }; 36 | 37 | /* --- Header --------------------------------------------------------------- */ 38 | /* Information passed from the kernel to the bootloader */ 39 | 40 | struct stivale2_header { 41 | _stivale2_split64(entry_point); 42 | _stivale2_split64(stack); 43 | uint64_t flags; 44 | _stivale2_split64(tags); 45 | }; 46 | 47 | #define STIVALE2_HEADER_TAG_ANY_VIDEO_ID 0xc75c9fa92a44c4db 48 | 49 | struct stivale2_header_tag_any_video { 50 | struct stivale2_tag tag; 51 | uint64_t preference; 52 | }; 53 | 54 | #define STIVALE2_HEADER_TAG_FRAMEBUFFER_ID 0x3ecc1bc43d0f7971 55 | 56 | struct stivale2_header_tag_framebuffer { 57 | struct stivale2_tag tag; 58 | uint16_t framebuffer_width; 59 | uint16_t framebuffer_height; 60 | uint16_t framebuffer_bpp; 61 | uint16_t unused; 62 | }; 63 | 64 | #define STIVALE2_HEADER_TAG_FB_MTRR_ID 0x4c7bb07731282e00 65 | 66 | #define STIVALE2_HEADER_TAG_SLIDE_HHDM_ID 0xdc29269c2af53d1d 67 | 68 | struct stivale2_header_tag_slide_hhdm { 69 | struct stivale2_tag tag; 70 | uint64_t flags; 71 | _stivale2_split64(alignment); 72 | }; 73 | 74 | #define STIVALE2_HEADER_TAG_TERMINAL_ID 0xa85d499b1823be72 75 | 76 | struct stivale2_header_tag_terminal { 77 | struct stivale2_tag tag; 78 | uint64_t flags; 79 | _stivale2_split64(callback); 80 | }; 81 | 82 | #define STIVALE2_TERM_CB_DEC 10 83 | #define STIVALE2_TERM_CB_BELL 20 84 | #define STIVALE2_TERM_CB_PRIVATE_ID 30 85 | #define STIVALE2_TERM_CB_STATUS_REPORT 40 86 | #define STIVALE2_TERM_CB_POS_REPORT 50 87 | #define STIVALE2_TERM_CB_KBD_LEDS 60 88 | #define STIVALE2_TERM_CB_MODE 70 89 | #define STIVALE2_TERM_CB_LINUX 80 90 | 91 | #define STIVALE2_TERM_CTX_SIZE ((uint64_t)(-1)) 92 | #define STIVALE2_TERM_CTX_SAVE ((uint64_t)(-2)) 93 | #define STIVALE2_TERM_CTX_RESTORE ((uint64_t)(-3)) 94 | #define STIVALE2_TERM_FULL_REFRESH ((uint64_t)(-4)) 95 | 96 | #define STIVALE2_HEADER_TAG_SMP_ID 0x1ab015085f3273df 97 | 98 | struct stivale2_header_tag_smp { 99 | struct stivale2_tag tag; 100 | uint64_t flags; 101 | }; 102 | 103 | #define STIVALE2_HEADER_TAG_5LV_PAGING_ID 0x932f477032007e8f 104 | 105 | #define STIVALE2_HEADER_TAG_UNMAP_NULL_ID 0x92919432b16fe7e7 106 | 107 | /* --- Struct --------------------------------------------------------------- */ 108 | /* Information passed from the bootloader to the kernel */ 109 | 110 | struct stivale2_struct { 111 | #define STIVALE2_BOOTLOADER_BRAND_SIZE 64 112 | char bootloader_brand[STIVALE2_BOOTLOADER_BRAND_SIZE]; 113 | 114 | #define STIVALE2_BOOTLOADER_VERSION_SIZE 64 115 | char bootloader_version[STIVALE2_BOOTLOADER_VERSION_SIZE]; 116 | 117 | uint64_t tags; 118 | }; 119 | 120 | #define STIVALE2_STRUCT_TAG_PMRS_ID 0x5df266a64047b6bd 121 | 122 | #define STIVALE2_PMR_EXECUTABLE ((uint64_t)1 << 0) 123 | #define STIVALE2_PMR_WRITABLE ((uint64_t)1 << 1) 124 | #define STIVALE2_PMR_READABLE ((uint64_t)1 << 2) 125 | 126 | struct stivale2_pmr { 127 | uint64_t base; 128 | uint64_t length; 129 | uint64_t permissions; 130 | }; 131 | 132 | struct stivale2_struct_tag_pmrs { 133 | struct stivale2_tag tag; 134 | uint64_t entries; 135 | struct stivale2_pmr pmrs[]; 136 | }; 137 | 138 | #define STIVALE2_STRUCT_TAG_KERNEL_BASE_ADDRESS_ID 0x060d78874a2a8af0 139 | 140 | struct stivale2_struct_tag_kernel_base_address { 141 | struct stivale2_tag tag; 142 | uint64_t physical_base_address; 143 | uint64_t virtual_base_address; 144 | }; 145 | 146 | #define STIVALE2_STRUCT_TAG_CMDLINE_ID 0xe5e76a1b4597a781 147 | 148 | struct stivale2_struct_tag_cmdline { 149 | struct stivale2_tag tag; 150 | uint64_t cmdline; 151 | }; 152 | 153 | #define STIVALE2_STRUCT_TAG_MEMMAP_ID 0x2187f79e8612de07 154 | 155 | #define STIVALE2_MMAP_USABLE 1 156 | #define STIVALE2_MMAP_RESERVED 2 157 | #define STIVALE2_MMAP_ACPI_RECLAIMABLE 3 158 | #define STIVALE2_MMAP_ACPI_NVS 4 159 | #define STIVALE2_MMAP_BAD_MEMORY 5 160 | #define STIVALE2_MMAP_BOOTLOADER_RECLAIMABLE 0x1000 161 | #define STIVALE2_MMAP_KERNEL_AND_MODULES 0x1001 162 | #define STIVALE2_MMAP_FRAMEBUFFER 0x1002 163 | 164 | struct stivale2_mmap_entry { 165 | uint64_t base; 166 | uint64_t length; 167 | uint32_t type; 168 | uint32_t unused; 169 | }; 170 | 171 | struct stivale2_struct_tag_memmap { 172 | struct stivale2_tag tag; 173 | uint64_t entries; 174 | struct stivale2_mmap_entry memmap[]; 175 | }; 176 | 177 | #define STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID 0x506461d2950408fa 178 | 179 | #define STIVALE2_FBUF_MMODEL_RGB 1 180 | 181 | struct stivale2_struct_tag_framebuffer { 182 | struct stivale2_tag tag; 183 | uint64_t framebuffer_addr; 184 | uint16_t framebuffer_width; 185 | uint16_t framebuffer_height; 186 | uint16_t framebuffer_pitch; 187 | uint16_t framebuffer_bpp; 188 | uint8_t memory_model; 189 | uint8_t red_mask_size; 190 | uint8_t red_mask_shift; 191 | uint8_t green_mask_size; 192 | uint8_t green_mask_shift; 193 | uint8_t blue_mask_size; 194 | uint8_t blue_mask_shift; 195 | uint8_t unused; 196 | }; 197 | 198 | #define STIVALE2_STRUCT_TAG_EDID_ID 0x968609d7af96b845 199 | 200 | struct stivale2_struct_tag_edid { 201 | struct stivale2_tag tag; 202 | uint64_t edid_size; 203 | uint8_t edid_information[]; 204 | }; 205 | 206 | #define STIVALE2_STRUCT_TAG_TEXTMODE_ID 0x38d74c23e0dca893 207 | 208 | struct stivale2_struct_tag_textmode { 209 | struct stivale2_tag tag; 210 | uint64_t address; 211 | uint16_t unused; 212 | uint16_t rows; 213 | uint16_t cols; 214 | uint16_t bytes_per_char; 215 | }; 216 | 217 | #define STIVALE2_STRUCT_TAG_FB_MTRR_ID 0x6bc1a78ebe871172 218 | 219 | #define STIVALE2_STRUCT_TAG_TERMINAL_ID 0xc2b3f4c3233b0974 220 | 221 | struct stivale2_struct_tag_terminal { 222 | struct stivale2_tag tag; 223 | uint32_t flags; 224 | uint16_t cols; 225 | uint16_t rows; 226 | uint64_t term_write; 227 | uint64_t max_length; 228 | }; 229 | 230 | #define STIVALE2_STRUCT_TAG_MODULES_ID 0x4b6fe466aade04ce 231 | 232 | struct stivale2_module { 233 | uint64_t begin; 234 | uint64_t end; 235 | 236 | #define STIVALE2_MODULE_STRING_SIZE 128 237 | char string[STIVALE2_MODULE_STRING_SIZE]; 238 | }; 239 | 240 | struct stivale2_struct_tag_modules { 241 | struct stivale2_tag tag; 242 | uint64_t module_count; 243 | struct stivale2_module modules[]; 244 | }; 245 | 246 | #define STIVALE2_STRUCT_TAG_RSDP_ID 0x9e1786930a375e78 247 | 248 | struct stivale2_struct_tag_rsdp { 249 | struct stivale2_tag tag; 250 | uint64_t rsdp; 251 | }; 252 | 253 | #define STIVALE2_STRUCT_TAG_EPOCH_ID 0x566a7bed888e1407 254 | 255 | struct stivale2_struct_tag_epoch { 256 | struct stivale2_tag tag; 257 | uint64_t epoch; 258 | }; 259 | 260 | #define STIVALE2_STRUCT_TAG_FIRMWARE_ID 0x359d837855e3858c 261 | 262 | #define STIVALE2_FIRMWARE_BIOS (1 << 0) 263 | 264 | struct stivale2_struct_tag_firmware { 265 | struct stivale2_tag tag; 266 | uint64_t flags; 267 | }; 268 | 269 | #define STIVALE2_STRUCT_TAG_EFI_SYSTEM_TABLE_ID 0x4bc5ec15845b558e 270 | 271 | struct stivale2_struct_tag_efi_system_table { 272 | struct stivale2_tag tag; 273 | uint64_t system_table; 274 | }; 275 | 276 | #define STIVALE2_STRUCT_TAG_KERNEL_FILE_ID 0xe599d90c2975584a 277 | 278 | struct stivale2_struct_tag_kernel_file { 279 | struct stivale2_tag tag; 280 | uint64_t kernel_file; 281 | }; 282 | 283 | #define STIVALE2_STRUCT_TAG_KERNEL_FILE_V2_ID 0x37c13018a02c6ea2 284 | 285 | struct stivale2_struct_tag_kernel_file_v2 { 286 | struct stivale2_tag tag; 287 | uint64_t kernel_file; 288 | uint64_t kernel_size; 289 | }; 290 | 291 | #define STIVALE2_STRUCT_TAG_BOOT_VOLUME_ID 0x9b4358364c19ee62 292 | 293 | struct stivale2_guid { 294 | uint32_t a; 295 | uint16_t b; 296 | uint16_t c; 297 | uint8_t d[8]; 298 | }; 299 | 300 | struct stivale2_struct_tag_boot_volume { 301 | struct stivale2_tag tag; 302 | uint64_t flags; 303 | struct stivale2_guid guid; 304 | struct stivale2_guid part_guid; 305 | }; 306 | 307 | #define STIVALE2_STRUCT_TAG_KERNEL_SLIDE_ID 0xee80847d01506c57 308 | 309 | struct stivale2_struct_tag_kernel_slide { 310 | struct stivale2_tag tag; 311 | uint64_t kernel_slide; 312 | }; 313 | 314 | #define STIVALE2_STRUCT_TAG_SMBIOS_ID 0x274bd246c62bf7d1 315 | 316 | struct stivale2_struct_tag_smbios { 317 | struct stivale2_tag tag; 318 | uint64_t flags; 319 | uint64_t smbios_entry_32; 320 | uint64_t smbios_entry_64; 321 | }; 322 | 323 | #define STIVALE2_STRUCT_TAG_SMP_ID 0x34d1d96339647025 324 | 325 | struct stivale2_smp_info { 326 | uint32_t processor_id; 327 | uint32_t lapic_id; 328 | uint64_t target_stack; 329 | uint64_t goto_address; 330 | uint64_t extra_argument; 331 | }; 332 | 333 | struct stivale2_struct_tag_smp { 334 | struct stivale2_tag tag; 335 | uint64_t flags; 336 | uint32_t bsp_lapic_id; 337 | uint32_t unused; 338 | uint64_t cpu_count; 339 | struct stivale2_smp_info smp_info[]; 340 | }; 341 | 342 | #define STIVALE2_STRUCT_TAG_PXE_SERVER_INFO 0x29d1e96239247032 343 | 344 | struct stivale2_struct_tag_pxe_server_info { 345 | struct stivale2_tag tag; 346 | uint32_t server_ip; 347 | }; 348 | 349 | #define STIVALE2_STRUCT_TAG_MMIO32_UART 0xb813f9b8dbc78797 350 | 351 | struct stivale2_struct_tag_mmio32_uart { 352 | struct stivale2_tag tag; 353 | uint64_t addr; 354 | }; 355 | 356 | #define STIVALE2_STRUCT_TAG_DTB 0xabb29bd49a2833fa 357 | 358 | struct stivale2_struct_tag_dtb { 359 | struct stivale2_tag tag; 360 | uint64_t addr; 361 | uint64_t size; 362 | }; 363 | 364 | #define STIVALE2_STRUCT_TAG_HHDM_ID 0xb0ed257db18cb58f 365 | 366 | struct stivale2_struct_tag_hhdm { 367 | struct stivale2_tag tag; 368 | uint64_t addr; 369 | }; 370 | 371 | #undef _stivale2_split64 372 | 373 | #endif 374 | --------------------------------------------------------------------------------