aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / ck_fb_check.c
| 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 | /* ck_fb_check -- host checker for the AIENOS_CK_SCREEN gate |
| 18 | * (scripts/qemu_ck_screen_test.sh): does a QEMU screendump show exactly |
| 19 | * what the kernel's framebuffer console (core/fbcon.c) must have drawn for |
| 20 | * the serial log of the same boot? Hosted C, test tooling only, never part |
| 21 | * of an image. No OCR: the expected screen is rendered by the kernel's own |
| 22 | * console code from the serial bytes, and the dump is decoded cell by cell |
| 23 | * against the kernel's own font. |
| 24 | * |
| 25 | * ck_fb_check --serial LOG [--ppm DUMP] [--render OUT.ppm] [--expect TEXT]... |
| 26 | * |
| 27 | * --render writes the screen rendered in step 2 as a PPM (evidence, and the |
| 28 | * gate's self-test input); without --ppm only steps 1-2 run. |
| 29 | * |
| 30 | * 1. LOG must hold exactly one line starting "screen: gop " (the first |
| 31 | * line the kernel draws, core/kmain.c screen_start); its geometry |
| 32 | * (WxH, format, scale, grid) must match DUMP (binary PPM, P6, maxval |
| 33 | * 255, as QMP screendump writes it) and what ck_fbcon_setup computes. |
| 34 | * 2. The serial bytes from that line to the end of LOG are fed through |
| 35 | * ck_fbcon_write into a host buffer; every visible pixel of DUMP must |
| 36 | * equal the rendered pixel (mismatched=0). |
| 37 | * 3. DUMP is decoded: each cell must be a font glyph or blank with uniform |
| 38 | * scale x scale blocks and black gap rows (undecodable=0); the rows are |
| 39 | * printed as "screen_row NNN|text|". |
| 40 | * 4. Each --expect TEXT must equal one decoded row (trailing blanks |
| 41 | * trimmed). |
| 42 | * Exit 0 only if all four hold (or the render was written); 1 otherwise; 2 |
| 43 | * usage or unreadable input. */ |
| 44 | #include <stdint.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | |
| 49 | #include "../core/fbcon.h" |
| 50 | #include "../core/font8x8.h" |
| 51 | |
| 52 | #define MAX_EXPECT 16 |
| 53 | |
| 54 | static unsigned char *slurp(const char *path, size_t *len) |
| 55 | { |
| 56 | FILE *f = fopen(path, "rb"); |
| 57 | if (!f) |
| 58 | return 0; |
| 59 | size_t cap = 1 << 16, n = 0; |
| 60 | unsigned char *b = malloc(cap + 1); |
| 61 | size_t r; |
| 62 | while (b && (r = fread(b + n, 1, cap - n, f)) > 0) { |
| 63 | n += r; |
| 64 | if (n == cap) { |
| 65 | cap *= 2; |
| 66 | unsigned char *nb = realloc(b, cap + 1); |
| 67 | if (!nb) { |
| 68 | free(b); |
| 69 | b = 0; |
| 70 | } |
| 71 | b = nb; |
| 72 | } |
| 73 | } |
| 74 | fclose(f); |
| 75 | if (b) |
| 76 | b[n] = 0; |
| 77 | *len = n; |
| 78 | return b; |
| 79 | } |
| 80 | |
| 81 | /* P6 header: "P6" ws W ws H ws 255 single-ws, no comments (QEMU writes none). */ |
| 82 | static const unsigned char *ppm_parse(const unsigned char *b, size_t len, uint32_t *w, uint32_t *h) |
| 83 | { |
| 84 | unsigned mv = 0; |
| 85 | int off = 0; |
| 86 | if (len < 16 || sscanf((const char *)b, "P6 %u %u %u%n", w, h, &mv, &off) != 3 || mv != 255) |
| 87 | return 0; |
| 88 | off++; /* the single whitespace byte after maxval */ |
| 89 | if ((uint64_t)off + (uint64_t)*w * *h * 3 > len || !*w || !*h) |
| 90 | return 0; |
| 91 | return b + off; |
| 92 | } |
| 93 | |
| 94 | static uint32_t to_pixel(const unsigned char *rgb, uint32_t format) |
| 95 | { |
| 96 | /* Memory bytes 0..2 of a 32-bit pixel: RGBX = R,G,B; BGRX = B,G,R; the |
| 97 | * uint32 is read little endian like the kernel stores it. */ |
| 98 | if (format == 0) |
| 99 | return (uint32_t)rgb[0] | (uint32_t)rgb[1] << 8 | (uint32_t)rgb[2] << 16; |
| 100 | return (uint32_t)rgb[2] | (uint32_t)rgb[1] << 8 | (uint32_t)rgb[0] << 16; |
| 101 | } |
| 102 | |
| 103 | static int usage(void) |
| 104 | { |
| 105 | fprintf(stderr, "usage: ck_fb_check --serial LOG [--ppm DUMP] [--render OUT.ppm] [--expect TEXT]...\n"); |
| 106 | return 2; |
| 107 | } |
| 108 | |
| 109 | static int write_ppm(const char *path, const uint32_t *buf, uint32_t w, uint32_t h, uint32_t format) |
| 110 | { |
| 111 | FILE *f = fopen(path, "wb"); |
| 112 | if (!f) |
| 113 | return -1; |
| 114 | fprintf(f, "P6\n%u %u\n255\n", w, h); |
| 115 | for (uint64_t i = 0; i < (uint64_t)w * h; i++) { |
| 116 | uint32_t v = buf[i]; |
| 117 | unsigned char rgb[3]; |
| 118 | if (format == 0) { |
| 119 | rgb[0] = v & 0xff, rgb[1] = (v >> 8) & 0xff, rgb[2] = (v >> 16) & 0xff; |
| 120 | } else { |
| 121 | rgb[2] = v & 0xff, rgb[1] = (v >> 8) & 0xff, rgb[0] = (v >> 16) & 0xff; |
| 122 | } |
| 123 | fwrite(rgb, 1, 3, f); |
| 124 | } |
| 125 | return fclose(f) == 0 ? 0 : -1; |
| 126 | } |
| 127 | |
| 128 | int main(int argc, char **argv) |
| 129 | { |
| 130 | const char *serial = 0, *ppm = 0, *render = 0, *expect[MAX_EXPECT]; |
| 131 | int n_expect = 0; |
| 132 | for (int i = 1; i < argc; i++) { |
| 133 | if (!strcmp(argv[i], "--serial") && i + 1 < argc) |
| 134 | serial = argv[++i]; |
| 135 | else if (!strcmp(argv[i], "--ppm") && i + 1 < argc) |
| 136 | ppm = argv[++i]; |
| 137 | else if (!strcmp(argv[i], "--render") && i + 1 < argc) |
| 138 | render = argv[++i]; |
| 139 | else if (!strcmp(argv[i], "--expect") && i + 1 < argc && n_expect < MAX_EXPECT) |
| 140 | expect[n_expect++] = argv[++i]; |
| 141 | else |
| 142 | return usage(); |
| 143 | } |
| 144 | if (!serial || (!ppm && !render)) |
| 145 | return usage(); |
| 146 | size_t slen = 0; |
| 147 | unsigned char *log = slurp(serial, &slen); |
| 148 | if (!log) { |
| 149 | printf("fb_check: cannot read %s\n", serial); |
| 150 | return 2; |
| 151 | } |
| 152 | |
| 153 | /* 1. the marker line, exactly once, at a line start */ |
| 154 | static const char marker[] = "screen: gop "; |
| 155 | const char *start = 0; |
| 156 | unsigned markers = 0; |
| 157 | for (size_t i = 0; i + sizeof marker - 1 <= slen; i++) |
| 158 | if ((i == 0 || log[i - 1] == '\n') && !memcmp(log + i, marker, sizeof marker - 1)) { |
| 159 | markers++; |
| 160 | if (!start) |
| 161 | start = (const char *)log + i; |
| 162 | } |
| 163 | if (markers != 1) { |
| 164 | printf("fb_check: FAIL %u \"screen: gop\" lines in the serial log (want exactly 1)\n", markers); |
| 165 | return 1; |
| 166 | } |
| 167 | uint32_t lw, lh, pitch, scale, cols, rows; |
| 168 | char fmt[8] = {0}; |
| 169 | unsigned long long base, bytes; |
| 170 | if (sscanf(start, "screen: gop %ux%u pitch=%u format=%4s base=0x%llx bytes=0x%llx scale=%u grid=%ux%u", |
| 171 | &lw, &lh, &pitch, fmt, &base, &bytes, &scale, &cols, &rows) != 9 || |
| 172 | (strcmp(fmt, "rgbx") && strcmp(fmt, "bgrx"))) { |
| 173 | printf("fb_check: FAIL malformed \"screen: gop\" line\n"); |
| 174 | return 1; |
| 175 | } |
| 176 | uint32_t format = !strcmp(fmt, "bgrx"); |
| 177 | |
| 178 | /* 2. render the serial bytes from the marker on with the kernel's code */ |
| 179 | static struct ck_fbcon con; |
| 180 | uint32_t w = lw, h = lh; |
| 181 | uint32_t *buf = calloc((size_t)w * h, 4); |
| 182 | if (!buf || ck_fbcon_setup(&con, buf, w, h, w, format) != 0) { |
| 183 | printf("fb_check: FAIL cannot set up a %ux%u console\n", w, h); |
| 184 | return 1; |
| 185 | } |
| 186 | if (con.scale != scale || con.cols != cols || con.rows != rows) { |
| 187 | printf("fb_check: FAIL geometry: serial scale=%u grid=%ux%u, console code scale=%u grid=%ux%u\n", scale, |
| 188 | cols, rows, con.scale, con.cols, con.rows); |
| 189 | return 1; |
| 190 | } |
| 191 | ck_fbcon_write(&con, start, slen - (size_t)(start - (const char *)log)); |
| 192 | if (render && write_ppm(render, buf, w, h, format) != 0) { |
| 193 | printf("fb_check: cannot write %s\n", render); |
| 194 | return 2; |
| 195 | } |
| 196 | if (!ppm) { |
| 197 | printf("fb_check: rendered %ux%u format=%s scale=%u grid=%ux%u scrolls=%llu to %s\n", w, h, fmt, scale, |
| 198 | cols, rows, (unsigned long long)con.scrolls, render); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | size_t plen = 0; |
| 203 | unsigned char *dump = slurp(ppm, &plen); |
| 204 | uint32_t dw = 0, dh = 0; |
| 205 | const unsigned char *px = dump ? ppm_parse(dump, plen, &dw, &dh) : 0; |
| 206 | if (!px) { |
| 207 | printf("fb_check: FAIL %s is missing or not a binary P6 PPM with maxval 255\n", ppm); |
| 208 | return 1; |
| 209 | } |
| 210 | if (dw != w || dh != h) { |
| 211 | printf("fb_check: FAIL geometry: serial %ux%u, dump %ux%u\n", w, h, dw, dh); |
| 212 | return 1; |
| 213 | } |
| 214 | uint64_t mismatched = 0; |
| 215 | for (uint32_t y = 0; y < h; y++) |
| 216 | for (uint32_t x = 0; x < w; x++) { |
| 217 | uint64_t i = (uint64_t)y * w + x; |
| 218 | if (to_pixel(px + 3 * i, format) != buf[i]) { |
| 219 | if (mismatched < 5) |
| 220 | printf("fb_check: pixel (%u,%u) dump=0x%06x rendered=0x%06x\n", x, y, |
| 221 | to_pixel(px + 3 * i, format), buf[i]); |
| 222 | mismatched++; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* 3. decode the dump against the font */ |
| 227 | uint64_t undecodable = 0; |
| 228 | char (*text)[CK_FBCON_MAX_COLS + 1] = calloc(rows, sizeof *text); |
| 229 | if (!text) |
| 230 | return 2; |
| 231 | uint32_t s = scale, cw = CK_FBCON_CELL_W * s, chh = CK_FBCON_CELL_H * s; |
| 232 | for (uint32_t r = 0; r < rows; r++) { |
| 233 | for (uint32_t k = 0; k < cols; k++) { |
| 234 | uint8_t bits[CK_FBCON_CELL_H] = {0}; |
| 235 | int bad = 0; |
| 236 | for (uint32_t cy = 0; cy < chh && !bad; cy++) |
| 237 | for (uint32_t cx = 0; cx < cw; cx++) { |
| 238 | uint64_t i = ((uint64_t)r * chh + cy) * w + (uint64_t)k * cw + cx; |
| 239 | uint32_t v = to_pixel(px + 3 * i, format); |
| 240 | if (v != CK_FBCON_FG && v != CK_FBCON_BG) { |
| 241 | bad = 1; |
| 242 | break; |
| 243 | } |
| 244 | int on = v == CK_FBCON_FG; |
| 245 | uint32_t gy = cy / s, gx = cx / s; |
| 246 | if (cy % s == 0 && cx % s == 0) { |
| 247 | if (on) |
| 248 | bits[gy] |= (uint8_t)(1u << gx); |
| 249 | } else if (on != ((bits[gy] >> gx) & 1)) { |
| 250 | bad = 1; /* scale x scale block not uniform */ |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | if (!bad && (bits[8] || bits[9])) |
| 255 | bad = 1; /* the two gap rows of a cell stay black */ |
| 256 | char ch = 0; |
| 257 | for (int g = 0; !bad && g <= CK_FONT_LAST - CK_FONT_FIRST && !ch; g++) |
| 258 | if (!memcmp(bits, ck_font8x8[g], CK_FONT_H)) |
| 259 | ch = (char)(CK_FONT_FIRST + g); |
| 260 | if (!ch) { |
| 261 | ch = '?'; /* shown as '?', counted; a real '?' glyph is not counted */ |
| 262 | undecodable++; |
| 263 | } |
| 264 | text[r][k] = ch; |
| 265 | } |
| 266 | uint32_t n = cols; |
| 267 | while (n && text[r][n - 1] == ' ') |
| 268 | n--; |
| 269 | text[r][n] = 0; |
| 270 | printf("screen_row %03u|%s|\n", r, text[r]); |
| 271 | } |
| 272 | |
| 273 | /* 4. expected rows */ |
| 274 | int found_all = 1; |
| 275 | for (int e = 0; e < n_expect; e++) { |
| 276 | int found = 0; |
| 277 | for (uint32_t r = 0; r < rows && !found; r++) |
| 278 | found = !strcmp(text[r], expect[e]); |
| 279 | printf("fb_check: expect \"%s\": %s\n", expect[e], found ? "found" : "MISSING"); |
| 280 | found_all &= found; |
| 281 | } |
| 282 | int ok = mismatched == 0 && undecodable == 0 && found_all; |
| 283 | printf("fb_check: %s %ux%u format=%s scale=%u grid=%ux%u scrolls=%llu pixels=%llu mismatched=%llu " |
| 284 | "undecodable=%llu\n", |
| 285 | ok ? "PASS" : "FAIL", w, h, fmt, scale, cols, rows, (unsigned long long)con.scrolls, |
| 286 | (unsigned long long)w * h, (unsigned long long)mismatched, (unsigned long long)undecodable); |
| 287 | return ok ? 0 : 1; |
| 288 | } |