Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/core/handoff_check.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / core / handoff_check.c` 154 lines ยท 6697 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* handoff_check.c -- refuse a malformed loader -> kernel handoff record 18 * before the kernel trusts any field of it (docs/BOOT_HANDOFF_CONTRACT.md 19 * section 6). Checked here: NULL record, magic (CHANDOF3 only; CHANDOF1 and 20 * CHANDOF2 are "unsupported version ", anything else "bad magic"), 21 * reserved0, reserved1, the CHANDOF2 model fields, the CHANDOF3 framebuffer 22 * fields, descriptor 23 * stride and version, memory map size. The other PROPOSED refusals of the 24 * contract (image range, overlaps, rsdp, firmware_el, CHANDOF version 25 * line) are not part of this cut. */ 26 #include "handoff_check.h" 27 #include "fmt.h" 28 29 /* TEST-ONLY mutant (tests: make test builds it and requires it to FAIL). 30 * Never in a kernel image or a hardware staging build. */ 31 #ifdef CK_HANDOFF_MUTANT_SKIP_RESERVED 32 #if !__STDC_HOSTED__ 33 #error "CK_HANDOFF_MUTANT_SKIP_RESERVED is a host test mutant only" 34 #endif 35 #ifdef CK_HARDWARE_STAGING 36 #error "CK_HANDOFF_MUTANT_SKIP_RESERVED cannot be combined with CK_HARDWARE_STAGING" 37 #endif 38 #endif 39 40 static char why_buf[80]; 41 42 static int refuse(const char **why, const char *what, unsigned long long n) 43 { 44 ck_snprintf(why_buf, sizeof why_buf, "%s %llu", what, n); 45 *why = why_buf; 46 return -1; 47 } 48 49 /* CHANDOF3 framebuffer block (docs/BOOT_HANDOFF_CONTRACT.md section 3). */ 50 static int fb_check(const struct ck_handoff *h, const char **why) 51 { 52 switch (h->fb_status) { 53 case CK_HANDOFF_FB_NONE: 54 if (h->fb_base || h->fb_size || h->fb_width || h->fb_height || h->fb_pitch || h->fb_format || 55 h->fb_gop_handles) { 56 *why = "fb fields without gop"; 57 return -1; 58 } 59 return 0; 60 case CK_HANDOFF_FB_NO_LINEAR: 61 if (h->fb_base || h->fb_size) 62 return refuse(why, "fb range without linear framebuffer", (unsigned long long)h->fb_size); 63 if (!h->fb_gop_handles) 64 return refuse(why, "bad fb gop handles", 0); 65 return 0; 66 case CK_HANDOFF_FB_PRESENT: 67 break; 68 default: 69 return refuse(why, "bad fb status", (unsigned long long)h->fb_status); 70 } 71 if (h->fb_format != CK_HANDOFF_FB_RGBX && h->fb_format != CK_HANDOFF_FB_BGRX) 72 return refuse(why, "bad fb format", (unsigned long long)h->fb_format); 73 if (!h->fb_width || h->fb_width > CK_HANDOFF_FB_MAX_DIM) 74 return refuse(why, "bad fb width", (unsigned long long)h->fb_width); 75 if (!h->fb_height || h->fb_height > CK_HANDOFF_FB_MAX_DIM) 76 return refuse(why, "bad fb height", (unsigned long long)h->fb_height); 77 if (h->fb_pitch < h->fb_width || h->fb_pitch > CK_HANDOFF_FB_MAX_DIM) 78 return refuse(why, "bad fb pitch", (unsigned long long)h->fb_pitch); 79 if (!h->fb_base || h->fb_base % 4) 80 return refuse(why, "bad fb base", (unsigned long long)h->fb_base); 81 /* UEFI 2.10 12.9.2: FrameBufferSize = PixelsPerScanLine x VerticalResolution 82 * x PixelElementSize (4 bytes for both accepted formats). At most 1 GiB. */ 83 uint64_t need = (uint64_t)h->fb_pitch * h->fb_height * 4u; 84 if (h->fb_size < need) 85 return refuse(why, "fb size too small", (unsigned long long)h->fb_size); 86 if (h->fb_base + h->fb_size < h->fb_base) 87 return refuse(why, "fb range overflow", (unsigned long long)h->fb_size); 88 if (!h->fb_gop_handles) 89 return refuse(why, "bad fb gop handles", 0); 90 return 0; 91 } 92 93 int ck_handoff_check(const struct ck_handoff *h, const char **why) 94 { 95 if (!h) { 96 *why = "null record"; 97 return -1; 98 } 99 if (h->magic != CK_HANDOFF_MAGIC) { 100 unsigned d = (unsigned)(h->magic >> 56); 101 if ((h->magic & 0x00ffffffffffffffull) == CK_HANDOFF_MAGIC_STEM && d >= '0' && d <= '9') { 102 ck_snprintf(why_buf, sizeof why_buf, "unsupported version %u (kernel reads CHANDOF3 only)", 103 d - '0'); 104 *why = why_buf; 105 return -1; 106 } 107 *why = "bad magic"; 108 return -1; 109 } 110 #ifndef CK_HANDOFF_MUTANT_SKIP_RESERVED 111 if (h->reserved0 != 0) { 112 *why = "reserved field nonzero"; 113 return -1; 114 } 115 #endif 116 if (h->reserved1 != 0) { 117 *why = "reserved1 field nonzero"; 118 return -1; 119 } 120 if (h->model_flags & ~(CK_HANDOFF_MODEL_PRESENT | CK_HANDOFF_MODEL_BLOCKIO)) 121 return refuse(why, "bad model flags", (unsigned long long)h->model_flags); 122 if (h->model_flags & CK_HANDOFF_MODEL_PRESENT) { 123 if (!h->model_base || !h->model_len) 124 return refuse(why, "bad model range", (unsigned long long)h->model_len); 125 if (h->model_base % 4096) 126 return refuse(why, "model base unaligned", (unsigned long long)h->model_base); 127 if (h->model_base + h->model_len < h->model_base) 128 return refuse(why, "model range overflow", (unsigned long long)h->model_len); 129 if (h->model_block_size != 512 && h->model_block_size != 4096) 130 return refuse(why, "bad model block size", (unsigned long long)h->model_block_size); 131 if (h->model_extents == 0) 132 return refuse(why, "bad model extents", 0); 133 } else { 134 int zero = h->model_base == 0 && h->model_len == 0 && h->model_flags == 0 && 135 h->model_extents == 0 && h->model_read_us == 0 && 136 h->model_disk_last_block == 0 && h->model_block_size == 0; 137 for (unsigned i = 0; zero && i < 32; i++) 138 zero = h->model_sha256[i] == 0; 139 if (!zero) { 140 *why = "model fields without model"; 141 return -1; 142 } 143 } 144 if (fb_check(h, why) != 0) 145 return -1; 146 if (h->desc_size < CK_HANDOFF_DESC_MIN || h->desc_size % 8 != 0) 147 return refuse(why, "bad descriptor size", (unsigned long long)h->desc_size); 148 if (h->desc_version != CK_HANDOFF_DESC_VERSION) 149 return refuse(why, "bad descriptor version", (unsigned long long)h->desc_version); 150 if (h->map_size == 0 || h->map_size > CK_HANDOFF_MAP_MAX || h->map_size % h->desc_size != 0) 151 return refuse(why, "bad map size", (unsigned long long)h->map_size); 152 *why = 0; 153 return 0; 154 }