~/bend-docscommunity

src/boot/image.bend source

src/boot/image.bend on the hub · documented module

import Baseimport ../lib/u64.bend as Uimport ../lib/bytes.bend as B# Booting an arm64 Linux kernel Image directly (Linux# Documentation/arch/arm64/booting.rst). The Image starts with a 64-byte# little-endian header:#   0  code0 u32      4  code1 u32     8  text_offset u64#   16 image_size u64 24 flags u64     32 res2 u64  40 res3 u64  48 res4 u64#   56 magic u32 = 1682788929 (0x644d5241, "ARM\x64")     60 res5 u32# An image_size of 0 means a kernel older than 3.17: text_offset is then# 524288 (0x80000) and the image size is unknown (use the file length).# Flags bit 0 is endianness (must be 0: little-endian).type ImageInfo is Data:  ImageInfo{text_offset: U.U64, image_size: U.U64, flags: U.U64}# Where everything goes in guest RAM.type Layout is Data:  Layout{kernel: U.U64, kernel_end: U.U64, dtb: U.U64, initrd: U.U64, initrd_end: U.U64}def Image.magic() -> U32:  1682788929   # 0x644d_5241, "ARM\x64"def Image.old_text_offset() -> U.U64:  U.U64{0, 524288}   # 0x8_0000def Image.mib2() -> U32:  2097152   # 0x20_0000# The le64 field at byte off of the header.def Image.field(bs: +List<U32>, off: Nat) -> U.U64:  B.Bytes.read_le64(B.Bytes.slice(bs, off, 8n))# A pre-3.17 kernel (image_size 0) loads at text_offset 0x8_0000.def Image.text_offset(old: Bool, given: U.U64) -> U.U64:  match old:    case True{}:      Image.old_text_offset()    case False{}:      givendef Image.when(-A: Data, ok: Bool, x: A) -> Maybe<&2, A>:  match ok:    case True{}:      Some{x}    case False{}:      None{}# The fields of a header of at least 64 bytes.def Image.parse.fields(+bs: +List<U32>) -> Maybe<&2, ImageInfo>:  +size +flags = Image.field(bs, 16n) Image.field(bs, 24n)  magic = B.Bytes.read_le32(B.Bytes.slice(bs, 56n, 4n))  le = U32.is_zero((U.U64.lo(flags) .&. 1 : U32))  text = Image.text_offset(U.U64.is_zero(size), Image.field(bs, 8n))  Image.when(ImageInfo, U32.is_eq(magic, Image.magic()) && le, ImageInfo{text, size, flags})def Image.parse.go(long: Bool, bs: +List<U32>) -> Maybe<&2, ImageInfo>:  match long:    case True{}:      Image.parse.fields(bs)    case False{}:      None{}# Parses the header; None if shorter than 64 bytes, the magic is wrong or the# kernel is big-endian.def Image.parse(header: +List<U32>) -> Maybe<&2, ImageInfo>:  +header = header  Image.parse.go(Nat.is_le(64n, List.length(&2, U32, header)), header)# a + b, or None when the sum wraps past 2^64.def Image.add(+a: U.U64, b: U.U64) -> Maybe<&2, U.U64>:  +s = U.U64.add(a, b)  Image.when(U.U64, U.U64.is_le(a, s), s)# a rounded up to a multiple of 2^k, or None when that wraps past 2^64.def Image.align(k: Nat, +a: U.U64) -> Maybe<&2, U.U64>:  +s = U.U64.align_up(k, a)  Image.when(U.U64, U.U64.is_le(a, s), s)def Image.max(+a: U.U64, +b: U.U64) -> U.U64:  Bool.pick(U.U64, U.U64.is_lt(a, b), b, a)# Places the kernel, the DTB and the initrd in RAM [ram_base, ram_base+ram_size):# - kernel at ram_base + text_offset (ram_base is 2 MiB aligned), spanning#   max(image_size, kernel_len) bytes;# - the DTB at the first 2 MiB boundary at or after the kernel's end (it must be#   8-byte aligned and within one 2 MiB region; dtb_len <= 2 MiB);# - the initrd at the first 4 KiB boundary at or after the end of the DTB's 2 MiB region#   (initrd_len 0 gives initrd == initrd_end, still placed there).# None if anything would end past the end of RAM.## Each region ends at or before the start of the next one, so the initrd's end# bounds them all. Also None when dtb_len > 2 MiB or an address wraps past 2^64.def Image.layout(ram_base: U.U64, ram_size: U.U64, info: ImageInfo, kernel_len: U32, dtb_len: U32, initrd_len: U32) -> Maybe<&2, Layout>:  match info:    case ImageInfo{text_offset, image_size, flags}:      +ram_base = ram_base      +dtb_len = dtb_len      do Maybe<&2, Layout>:        +ram_end : U.U64 <- Image.add(ram_base, ram_size)        +kernel : U.U64 <- Image.add(ram_base, text_offset)        +kernel_end : U.U64 <- Image.add(kernel, Image.max(image_size, U.U64{0, kernel_len}))        +dtb : U.U64 <- Image.align(21n, kernel_end)        # dtb + 2 MiB is 2 MiB aligned, so 4 KiB aligned too.        +initrd : U.U64 <- Image.add(dtb, U.U64{0, Image.mib2()})        +initrd_end : U.U64 <- Image.add(initrd, U.U64{0, initrd_len})        ok : Bool = U32.is_le(dtb_len, Image.mib2()) && U.U64.is_le(initrd_end, ram_end)        Image.when(Layout, ok, Layout{kernel, kernel_end, dtb, initrd, initrd_end})# A gzip stream (RFC 1952: 1f 8b), from a file's first bytes: a compressed# kernel (Ubuntu's vmlinuz) the host inflates into RAM. A file of 2 bytes or# fewer is not one.def Image.is_gzip(+head: +List<U32>) -> Bool:  Nat.is_le(3n, List.length(&2, U32, head)) && U32.is_eq(Maybe.default(&2, U32, List.get(&2, U32, head, 0n), 0), 31)    && U32.is_eq(Maybe.default(&2, U32, List.get(&2, U32, head, 1n), 0), 139)# An EFI zboot image (Linux's CONFIG_EFI_ZBOOT: the arm64 vmlinuz of# Alpine, Fedora and Debian): "zimg" at byte 4, the payload's offset and# size at bytes 8 and 12 (u32, little-endian), its compression's name at# byte 24. A gzip payload is a compressed Image, inflated into RAM as a# gzip kernel is.type Zboot is Data:  Zboot{off: U32, len: U32}def Image.byte(+head: +List<U32>, +n: Nat) -> U32:  Maybe.default(&2, U32, List.get(&2, U32, head, n), 256)def Image.le32(+head: +List<U32>, +n: Nat) -> U32:  (((Image.byte(head, n) .|. (Image.byte(head, Nat.add(n, 1n)) << 8n : U32) : U32) .|. (Image.byte(head, Nat.add(n, 2n)) << 16n : U32) : U32) .|. (Image.byte(head, Nat.add(n, 3n)) << 24n : U32) : U32)# The four bytes at n spell a, b, c, d.def Image.tag(+head: +List<U32>, +n: Nat, a: U32, b: U32, c: U32, d: U32) -> Bool:  U32.is_eq(Image.byte(head, n), a) && U32.is_eq(Image.byte(head, Nat.add(n, 1n)), b) && U32.is_eq(Image.byte(head, Nat.add(n, 2n)), c) && U32.is_eq(Image.byte(head, Nat.add(n, 3n)), d)# From a file's first 28 bytes: a zboot image's gzip payload (offset,# size), or none (not one, another compression, or an empty payload).def Image.zboot(+head: +List<U32>) -> Maybe<&2, Zboot>:  # "zimg" (7a 69 6d 67), "gzip" (67 7a 69 70)  +ok = Image.tag(head, 4n, 122, 105, 109, 103) && Image.tag(head, 24n, 103, 122, 105, 112) && U32.is_ne(Image.le32(head, 12n), 0)  Bool.pick(Maybe<&2, Zboot>, ok, Some{Zboot{Image.le32(head, 8n), Image.le32(head, 12n)}}, None{})