Introduction
The RISC-V Image is the uncompressed Linux kernel for RISC-V, with a 64-byte header that follows the layout of the
ARM64 one. The fields are from the Linux document “Boot image header in RISC-V Linux” (checked
against the document’s raw text; the descriptions are in our own words).
Header
The document gives text_offset, image_size, flags and the magic numbers as little-endian.
| Offset | Size | Field | What it holds |
|---|---|---|---|
0x00 | 4 | code0 | Executable code. The document says it is replaced by MZ for the EFI stub. |
0x04 | 4 | code1 | Executable code. |
0x08 | 8 | text_offset | The offset at which the image is loaded. |
0x10 | 8 | image_size | The effective size of the image. Mandatory: the kernel does not boot without it. |
0x18 | 8 | flags | Kernel flags. Bit 0 is the endianness (1 big-endian); no other bit is defined. |
0x20 | 4 | version | The header version: minor in bits 0–15, major in bits 16–31. 0.2 is the current one. |
0x24 | 4 | res1 | Reserved, zero. |
0x28 | 8 | res2 | Reserved, zero. |
0x30 | 8 | magic | 0x5643534952: the characters RISCV and zero padding. Deprecated from header version 0.2. |
0x38 | 4 | magic2 | 0x05435352: RSC followed by 0x05. It replaces magic. |
0x3C | 4 | res3 | Reserved for the offset of the rest of the PE/COFF header when the image has an EFI stub. |
A real image
The Debian stable installer for riscv64 has the kernel debian-installer/riscv64/linux (31,165,952 bytes), which is an Image with an EFI stub.
Its header: code0 bytes 4D 5A 6F 10, text_offset 0x200000, image_size 0x1E6B000
(31,895,552 bytes: larger than the file, which is allowed because the kernel clears memory after it), flags 0 (little-endian), version 2 (header 0.2),
magic RISCV, magic2 RSC\x05, res3 0x40. PE machine 0x5064 (RISC-V 64-bit), EFI application,
entry point 0xC54234, 4 KiB of headers, two sections .text and .data; llvm-readobj gives the same values.
The two letters MZ are also code. 4D 5A is a 16-bit compressed instruction, c.li s4, -13, so the next instruction starts at offset 2, not 4:
6F 10 60 0D is j (jal x0) to 0x10D8, the 32-bit jump that starts the kernel (llvm-mc reads those six bytes as the same two instructions). The viewer
walks the first eight bytes by instruction length, and says which instruction starts in code0 and which in code1.
What the viewer shows
The header field by field, the version, the endianness flag, the EFI stub offset, and the first 1,024 bytes (an option) disassembled with the
RISC-V decoder. The file does not say whether the kernel is RV32 or RV64 or which extensions it uses, so the options
bar chooses a profile: RV64GC by default. The viewer reports an image size of zero or below the file size, flag bits other than bit 0, a header version newer
than 0.2, and a version below 0.2 without the first magic. The Linux document does not say how a boot loader starts the image; that is outside these pages.
The file is recognised by magic2 and a non-zero image_size.
Not described
Known, not yet covered
- Viewer: the kernel after the first bytes is not explained: code and data are not told apart and no function is named.
- Reference: the PE/COFF data directories of an EFI-stub image (the viewer reads the machine, subsystem, entry point and section table; the PE format is on the EXE pages), and the way a boot loader hands over (the document for it is the RISC-V Linux boot requirements, which these pages do not cover).
- Viewer: only one real RISC-V Image (an RV64 one from Debian) has been run through it; no big-endian image, no image of header version 0.0 or 0.1 and no RV32 one.
Unknown
- Viewer: RV32 or RV64 and the extensions are not in the file: the options bar chooses, and the viewer cannot.