Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/cross-build-loongarch64.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Cross-build loongarch64 kernel
on: [pull_request, create]

jobs:
build:
if: github.event_name == 'pull_request'
name: Cross-build loongarch64 kernel
runs-on: ubuntu-24.04
steps:
- name: Code checkout
uses: actions/checkout@v2

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y make gcc bc bison flex elfutils python3-pyelftools curl patch libelf-dev gcc-loongarch64-linux-gnu

- name: Build loongarch64 kernel
run: make ARCH=loongarch CROSS_COMPILE=loongarch64-linux-gnu-
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ else ifeq ($(ARCH),riscv)
GUESTARCH := riscv64
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
else ifeq ($(ARCH),loongarch)
GUESTARCH := loongarch64
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
else
GUESTARCH := $(ARCH)
CC := $(CROSS_COMPILE)gcc
Expand All @@ -44,10 +48,13 @@ endif
KBUNDLE_TYPE_x86_64 = vmlinux
KBUNDLE_TYPE_aarch64 = Image
KBUNDLE_TYPE_riscv64 = Image
KBUNDLE_TYPE_loongarch64 = linux_pe

KERNEL_BINARY_x86_64 = $(KERNEL_SOURCES)/vmlinux
KERNEL_BINARY_aarch64 = $(KERNEL_SOURCES)/arch/arm64/boot/Image
KERNEL_BINARY_riscv64 = $(KERNEL_SOURCES)/arch/riscv/boot/Image
KERNEL_BINARY_loongarch64 = $(KERNEL_SOURCES)/arch/loongarch/boot/vmlinux.efi


KRUNFW_BINARY_Linux = libkrunfw$(VARIANT).so.$(FULL_VERSION)
KRUNFW_SONAME_Linux = libkrunfw$(VARIANT).so.$(ABI_VERSION)
Expand Down
37 changes: 31 additions & 6 deletions bin2cbundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
# Use 64k page size for rounding. This should cover 4k/16k/64k kernels
PAGE_SIZE = 65536
AARCH64_LOAD_ADDR = '0x80000000'
LINUX_PE_HEADER_SIZE = 64
LINUX_PE_MAGIC = 0x818223cd
LINUX_PE_KERNEL_ENTRY_OFFSET = 8
LINUX_PE_LOAD_OFFSET_OFFSET = 24
LINUX_PE_MAGIC_OFFSET = 56
LOONGARCH_DRAM_START = 0x40000000
LOONGARCH_VMLINUX_LOAD_ADDRESS = 0x9000000000200000

def write_header(ofile, bundle_name):
ofile.write('#include <stddef.h>\n')
Expand Down Expand Up @@ -69,11 +76,27 @@ def write_elf_cbundle(ifile, ofile) -> int:


def write_raw_cbundle(ifile, ofile) -> int:
data = ifile.read()
write_data_cbundle(data, ofile)

def write_linux_pe_cbundle(ifile, ofile) -> int:
data = ifile.read()
assert(len(data) >= LINUX_PE_HEADER_SIZE)
kernel_entry = data[LINUX_PE_KERNEL_ENTRY_OFFSET:LINUX_PE_KERNEL_ENTRY_OFFSET + 8]
load_offset = data[LINUX_PE_LOAD_OFFSET_OFFSET:LINUX_PE_LOAD_OFFSET_OFFSET + 8]
magic = data[LINUX_PE_MAGIC_OFFSET:LINUX_PE_MAGIC_OFFSET + 4]
assert(int.from_bytes(magic, 'little') == LINUX_PE_MAGIC)
image_load_addr = LOONGARCH_DRAM_START + int.from_bytes(load_offset, 'little')
entry_offset = int.from_bytes(kernel_entry, 'little') - LOONGARCH_VMLINUX_LOAD_ADDRESS
entry_addr = image_load_addr + entry_offset
write_data_cbundle(data, ofile)
return hex(image_load_addr), hex(entry_addr)

def write_data_cbundle(data, ofile):
col = 0
total_size = 0
byte = ifile.read(1)
while byte:
ofile.write('\\x{:x}'.format(byte[0]))
for byte in data:
ofile.write('\\x{:x}'.format(byte))

if col == 15:
ofile.write('"\n"')
Expand All @@ -82,13 +105,10 @@ def write_raw_cbundle(ifile, ofile) -> int:
col = col + 1

total_size = total_size + 1
byte = ifile.read(1)

rounded_size = int((total_size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE
padding = rounded_size - total_size
write_padding(ofile, padding, col)


def write_footer_generic(ofile, bundle_name):
footer = """
char * krunfw_get_{}(size_t *size)
Expand Down Expand Up @@ -145,6 +165,9 @@ def main() -> int:
elif args.t == 'initrd':
bundle_name = 'INITRD'
ifmt = 'raw'
elif args.t == 'linux_pe':
bundle_name = 'KERNEL'
ifmt = 'linux_pe'
else:
print('Invalid bundle type')
return -1
Expand All @@ -158,6 +181,8 @@ def main() -> int:
load_addr, entry_addr = write_elf_cbundle(ifile, ofile)
elif ifmt == 'raw':
write_raw_cbundle(ifile, ofile)
elif ifmt == 'linux_pe':
load_addr, entry_addr = write_linux_pe_cbundle(ifile, ofile)

if bundle_name == 'KERNEL':
if ifmt == 'raw':
Expand Down
Loading
Loading