From 63c1720e60d45deb956dd6f5bfda10f48ec64239 Mon Sep 17 00:00:00 2001 From: Freywar Ulvnaudgari Date: Tue, 25 Aug 2026 21:11:15 +0300 Subject: [PATCH] Improve installation process `meson.build` rewritten with DRY principle. `deploy.sh` added, which deploys the image on a installation USB stick borrowed from another project. Image name aligned with the installer's expectations. --- build.sh | 32 ++-- deploy.sh | 19 +++ meson.build | 404 ++++++--------------------------------------- src/user/linker.ld | 7 +- vm.sh | 2 +- 5 files changed, 91 insertions(+), 373 deletions(-) create mode 100755 deploy.sh diff --git a/build.sh b/build.sh index 0739df7..fbebb83 100755 --- a/build.sh +++ b/build.sh @@ -18,19 +18,19 @@ meson compile -v -C build sector_size=512 partition_offset=2048 -dd if=/dev/zero of=build/os.img bs="${sector_size}" count="$((partition_offset + 20480))" -dd if=build/mbr.bin of=build/os.img bs="${sector_size}" count=1 conv=notrunc -dd if=build/bootloader.bin of=build/os.img bs="${sector_size}" seek=1 count="$((partition_offset - 1))" conv=notrunc -mkfs.fat -F 16 --offset "${partition_offset}" build/os.img -mcopy -i build/os.img@@"$((partition_offset * sector_size))" -s src ::src -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/kernel.bin ::kernel.bin -mmd -i build/os.img@@"$((partition_offset * sector_size))" ::bin -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/terminal ::bin/terminal -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/shell ::bin/shell -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/echo ::bin/echo -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/ls ::bin/ls -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/cat ::bin/cat -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/cp ::bin/cp -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/mkdir ::bin/mkdir -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/rm ::bin/rm -mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/wc ::bin/wc +dd if=/dev/zero of=build/os.raw bs="${sector_size}" count="$((partition_offset + 20480))" +dd if=build/mbr.bin of=build/os.raw bs="${sector_size}" count=1 conv=notrunc +dd if=build/bootloader.bin of=build/os.raw bs="${sector_size}" seek=1 count="$((partition_offset - 1))" conv=notrunc +mkfs.fat -F 16 --offset "${partition_offset}" build/os.raw +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" -s src ::src +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/kernel.bin ::kernel.bin +mmd -i build/os.raw@@"$((partition_offset * sector_size))" ::bin +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/terminal ::bin/terminal +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/shell ::bin/shell +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/echo ::bin/echo +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/ls ::bin/ls +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/cat ::bin/cat +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/cp ::bin/cp +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/mkdir ::bin/mkdir +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/rm ::bin/rm +mcopy -i build/os.raw@@"$((partition_offset * sector_size))" build/wc ::bin/wc diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..4167cc4 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +device=${1:-} +if [[ ! -b ${device} ]]; then + echo "ERROR: \"${device}\" is not a block device" >&2 +fi + +if [[ ! -f build/os.raw ]]; then + echo "ERROR: \"build/os.raw\" does not appear to be built, run \"build.sh\"" >&2 +fi + +echo 'Copying the image...' >&2 +mnt=$(mktemp --directory) +mount "${device}2" "${mnt}" +cp --update "build/os.raw" "${mnt}" +umount "${mnt}" +rm -rf "${mnt}" +echo 'Done.' >&2 diff --git a/meson.build b/meson.build index 5a19d2c..9a52214 100644 --- a/meson.build +++ b/meson.build @@ -92,20 +92,25 @@ kernel_sources = files([ 'src/kernel/xhci.c', ]) +c_args = [ + '-ffreestanding', + '-nostdlib', + '-nostartfiles', + '-mno-red-zone', + '-mgeneral-regs-only', + '-fno-stack-protector', + '-DVERSION="' + meson.project_version() + '"', +] + kernel_elf = executable( 'kernel.elf', sources: [kernel_entry_o, timer_o, process_o, syscall_o, lib_sources, kernel_sources], c_args: [ - '-ffreestanding', - '-nostdlib', - '-nostartfiles', - '-mno-red-zone', - '-mgeneral-regs-only', + c_args, '-mcmodel=kernel', '-Wno-unused-command-line-argument', '-Wconversion', '-DKERNEL="yes"', - '-DVERSION="' + meson.project_version() + '"', ], link_args: [ '-T', meson.project_source_root() / 'src/kernel/linker.ld', @@ -122,353 +127,48 @@ custom_target( build_by_default: true, ) -terminal_start = custom_target( - 'terminal_start', - input: 'src/user/start.asm', - output: 'terminal_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) +foreach app : ['terminal', 'shell', 'echo', 'ls', 'cat', 'cp', 'mkdir', 'rm', 'wc'] + start = custom_target( + f'@app@_start', + input: 'src/user/start.asm', + output: f'@app@_start.o', + command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], + ) -terminal_syscall = custom_target( - 'terminal_syscall', - input: 'src/user/syscall.asm', - output: 'terminal_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) + syscall = custom_target( + f'@app@_syscall', + input: 'src/user/syscall.asm', + output: f'@app@_syscall.o', + command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], + ) -terminal_elf = executable( - 'terminal.elf', - sources: [terminal_start, terminal_syscall, lib_sources, 'src/user/app/terminal/terminal.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) + elf = executable( + f'@app@.elf', + sources: [start, syscall, lib_sources, f'src/user/app/@app@/@app@.c'], + c_args: [ + c_args, + '-mcmodel=large', + ], + link_args: [ + '-T', meson.project_source_root() / 'src/user/linker.ld', + '-nostdlib', + ], + link_depends: 'src/user/linker.ld', + ) -custom_target( - 'terminal', - input: terminal_elf, - output: 'terminal', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) + elf_data = custom_target( + f'@app@_data.elf', + input: elf, + output: f'@app@_data.elf', + command: ['objcopy', '--set-section-flags', '.data=alloc,load,contents', '@INPUT@', '@OUTPUT@'], + build_by_default: true, + ) -shell_start = custom_target( - 'shell_start', - input: 'src/user/start.asm', - output: 'shell_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -shell_syscall = custom_target( - 'shell_syscall', - input: 'src/user/syscall.asm', - output: 'shell_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -shell_elf = executable( - 'shell.elf', - sources: [shell_start, shell_syscall, lib_sources, 'src/user/app/shell/shell.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'shell', - input: shell_elf, - output: 'shell', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -echo_start = custom_target( - 'echo_start', - input: 'src/user/start.asm', - output: 'echo_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -echo_syscall = custom_target( - 'echo_syscall', - input: 'src/user/syscall.asm', - output: 'echo_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -echo_elf = executable( - 'echo.elf', - sources: [echo_start, echo_syscall, lib_sources, 'src/user/app/echo/echo.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'echo', - input: echo_elf, - output: 'echo', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -ls_start = custom_target( - 'ls_start', - input: 'src/user/start.asm', - output: 'ls_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -ls_syscall = custom_target( - 'ls_syscall', - input: 'src/user/syscall.asm', - output: 'ls_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -ls_elf = executable( - 'ls.elf', - sources: [ls_start, ls_syscall, lib_sources, 'src/user/app/ls/ls.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'ls', - input: ls_elf, - output: 'ls', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -cat_start = custom_target( - 'cat_start', - input: 'src/user/start.asm', - output: 'cat_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -cat_syscall = custom_target( - 'cat_syscall', - input: 'src/user/syscall.asm', - output: 'cat_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -cat_elf = executable( - 'cat.elf', - sources: [cat_start, cat_syscall, lib_sources, 'src/user/app/cat/cat.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'cat', - input: cat_elf, - output: 'cat', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -cp_start = custom_target( - 'cp_start', - input: 'src/user/start.asm', - output: 'cp_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -cp_syscall = custom_target( - 'cp_syscall', - input: 'src/user/syscall.asm', - output: 'cp_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -cp_elf = executable( - 'cp.elf', - sources: [cp_start, cp_syscall, lib_sources, 'src/user/app/cp/cp.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'cp', - input: cp_elf, - output: 'cp', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -mkdir_start = custom_target( - 'mkdir_start', - input: 'src/user/start.asm', - output: 'mkdir_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -mkdir_syscall = custom_target( - 'mkdir_syscall', - input: 'src/user/syscall.asm', - output: 'mkdir_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -mkdir_elf = executable( - 'mkdir.elf', - sources: [mkdir_start, mkdir_syscall, lib_sources, 'src/user/app/mkdir/mkdir.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'mkdir', - input: mkdir_elf, - output: 'mkdir', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -rm_start = custom_target( - 'rm_start', - input: 'src/user/start.asm', - output: 'rm_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -rm_syscall = custom_target( - 'rm_syscall', - input: 'src/user/syscall.asm', - output: 'rm_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -rm_elf = executable( - 'rm.elf', - sources: [rm_start, rm_syscall, lib_sources, 'src/user/app/rm/rm.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'rm', - input: rm_elf, - output: 'rm', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) - -wc_start = custom_target( - 'wc_start', - input: 'src/user/start.asm', - output: 'wc_start.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -wc_syscall = custom_target( - 'wc_syscall', - input: 'src/user/syscall.asm', - output: 'wc_syscall.o', - command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], -) - -wc_elf = executable( - 'wc.elf', - sources: [wc_start, wc_syscall, lib_sources, 'src/user/app/wc/wc.c'], - c_args: [ - '-ffreestanding', - '-nostdlib', - '-fno-stack-protector', - '-mcmodel=large', - '-DVERSION="' + meson.project_version() + '"', - ], - link_args: [ - '-T', meson.project_source_root() / 'src/user/linker.ld', - '-nostdlib', - ], - link_depends: 'src/user/linker.ld', -) - -custom_target( - 'wc', - input: wc_elf, - output: 'wc', - command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], - build_by_default: true, -) + custom_target( + app, + input: elf_data, + output: app, + command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], + build_by_default: true, + ) +endforeach diff --git a/src/user/linker.ld b/src/user/linker.ld index 5ff9262..73b9e1e 100644 --- a/src/user/linker.ld +++ b/src/user/linker.ld @@ -3,8 +3,7 @@ ENTRY(_start) SECTIONS { . = 0x0000000000400000; - .text : { *(.text) } - .rodata : { *(.rodata) } - .data : { *(.data) } - .bss : { *(.bss) } + .text : { *(.text*) } + .rodata : { *(.rodata*) *(.lrodata*) } + .data : { *(.data*) *(.ldata*) *(.bss*) *(.lbss*) } } diff --git a/vm.sh b/vm.sh index e9d0b6c..fc92ad4 100755 --- a/vm.sh +++ b/vm.sh @@ -9,7 +9,7 @@ fi qemu-system-x86_64 \ "${debug_flags[@]}" \ -monitor stdio \ - -drive file=build/os.img,format=raw,if=none,id=nvme0 \ + -drive file=build/os.raw,format=raw,if=none,id=nvme0 \ -device nvme,drive=nvme0,serial=foo \ -device qemu-xhci,id=xhci,msi=off,msix=off \ -device usb-kbd,bus=xhci.0 \