Create HOS.img with a GPT partition table and EFI partition

This commit is contained in:
Josh Holtrop 2022-08-20 19:01:25 -04:00
parent 93763917c1
commit b4a4638a82

View File

@ -10,6 +10,7 @@ configure do
check_c_compiler "x86_64-elf-gcc", use: "x86_64-elf-gcc" check_c_compiler "x86_64-elf-gcc", use: "x86_64-elf-gcc"
check_c_compiler check_c_compiler
check_program "mformat", on_fail: "Install the mtools package" check_program "mformat", on_fail: "Install the mtools package"
check_program "parted"
check_cfg package: "freetype2", on_fail: "Install libfreetype-dev", use: "freetype" check_cfg package: "freetype2", on_fail: "Install libfreetype-dev", use: "freetype"
sh %w[git submodule update --init] sh %w[git submodule update --init]
end end
@ -17,6 +18,12 @@ end
# Kernel default font size. # Kernel default font size.
KFONT_SIZE = 18 KFONT_SIZE = 18
# One kilobyte.
KB = 1024
# One megabyte.
MB = 1024 * 1024
class HulkBinObj < Builder class HulkBinObj < Builder
def run(options) def run(options)
FileUtils.mkdir_p(File.dirname(@target)) FileUtils.mkdir_p(File.dirname(@target))
@ -38,15 +45,24 @@ EOF
end end
end end
# Create a GPT disk image with an EFI partition containing the EFI image.
class Image < Builder class Image < Builder
def run(options) def run(options)
unless @cache.up_to_date?(@target, nil, @sources, @env) unless @cache.up_to_date?(@target, nil, @sources, @env)
print_run_message("Creating disk image <target>#{@target}<reset>", nil) print_run_message("Creating disk image <target>#{@target}<reset>", nil)
File.binwrite(@target, "\0" * (1440 * 1024)) efi_image_size = File.stat(@sources.first).size
system(*%W[mformat -i #{@target} -f 1440 ::]) efi_image_size_mb = (efi_image_size + MB - 1) / MB
partition_size_mb = efi_image_size_mb + 1
empty_mb = "\0".b * MB
File.binwrite(@target, empty_mb * partition_size_mb)
system(*%W[mformat -i #{@target} ::])
system(*%W[mmd -i #{@target} ::/EFI]) system(*%W[mmd -i #{@target} ::/EFI])
system(*%W[mmd -i #{@target} ::/EFI/BOOT]) system(*%W[mmd -i #{@target} ::/EFI/BOOT])
system(*%W[mcopy -i #{@target} #{@sources.first} ::/EFI/BOOT]) system(*%W[mcopy -i #{@target} #{@sources.first} ::/EFI/BOOT])
partition_contents = File.binread(@target)
disk_image = empty_mb + partition_contents + empty_mb
File.binwrite(@target, disk_image)
system(*%W[parted --script #{@target} mklabel gpt mkpart HOS fat32 1MiB #{partition_size_mb + 1}MiB])
@cache.register_build(@target, nil, @sources, @env) @cache.register_build(@target, nil, @sources, @env)
end end
true true