interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / ui / tools / boot_show_check.c

153 lines · 5729 bytes Raw Blame History
1/* boot_show_check.c -- host checker for the boot sequence QEMU gates
2 * (scripts/qemu_ck_boot_show_test.sh):
3 * boot_show_check wav FILE the sound a VM played (QEMU -audiodev wav):
4 * 48 kHz stereo S16 expected; finds where the chirp starts and
5 * compares every frame of it with ck_chirp_fill (ui/chirp.c), the
6 * same formula the kernel played from. Prints one line, exit 0 when
7 * the chirp is there whole (correlation >= 0.99), 1 otherwise.
8 * boot_show_check ppm FILE a QEMU screendump (P6): prints its size, the
9 * share of lit pixels and the brightest pixel; exit 0 when the screen
10 * is not blank (stars are lit: > 0.05% of pixels above 64).
11 * Integer and double math on the host only. */
12#include "../chirp.h"
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18static unsigned char *slurp(const char *path, long *n)
19{
20 FILE *f = fopen(path, "rb");
21 if (!f)
22 return 0;
23 fseek(f, 0, SEEK_END);
24 *n = ftell(f);
25 fseek(f, 0, SEEK_SET);
26 unsigned char *b = malloc((size_t)*n + 1);
27 if (!b || fread(b, 1, (size_t)*n, f) != (size_t)*n) {
28 fclose(f);
29 free(b);
30 return 0;
31 }
32 fclose(f);
33 return b;
34}
35
36static uint32_t le32(const unsigned char *p) { return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; }
37static uint16_t le16(const unsigned char *p) { return (uint16_t)(p[0] | p[1] << 8); }
38
39static int wav(const char *path)
40{
41 long n;
42 unsigned char *b = slurp(path, &n);
43 if (!b || n < 44 || memcmp(b, "RIFF", 4) || memcmp(b + 8, "WAVE", 4)) {
44 printf("wav: %s unreadable or not a WAVE file\n", path);
45 return 1;
46 }
47 /* walk the chunks for fmt and data */
48 long at = 12, data = -1, dlen = 0;
49 unsigned ch = 0, rate = 0, bits = 0;
50 while (at + 8 <= n) {
51 uint32_t len = le32(b + at + 4);
52 if (!memcmp(b + at, "fmt ", 4) && len >= 16)
53 ch = le16(b + at + 10), rate = le32(b + at + 12), bits = le16(b + at + 22);
54 if (!memcmp(b + at, "data", 4)) {
55 data = at + 8, dlen = (long)len;
56 if (data + dlen > n || dlen == 0)
57 dlen = n - data; /* QEMU fills in the length at close; a killed VM leaves 0 */
58 break;
59 }
60 at += 8 + len + (len & 1);
61 }
62 if (data < 0 || ch != 2 || rate != CK_CHIRP_RATE || bits != 16) {
63 printf("wav: format %u ch %u Hz %u bits (want 2 ch %u Hz 16 bits)\n", ch, rate, bits, CK_CHIRP_RATE);
64 return 1;
65 }
66 long frames = dlen / 4;
67 const int16_t *s = (const int16_t *)(const void *)(b + data);
68 /* the chirp's first audible frame in the reference and in the capture */
69 long ref0 = -1, cap0 = -1;
70 int16_t *ref = malloc(sizeof(int16_t) * 2 * CK_CHIRP_FRAMES);
71 ck_chirp_fill(ref, 0, CK_CHIRP_FRAMES);
72 for (long i = 0; i < (long)CK_CHIRP_FRAMES && ref0 < 0; i++)
73 if (abs(ref[2 * i]) > 200)
74 ref0 = i;
75 for (long i = 0; i < frames && cap0 < 0; i++)
76 if (abs(s[2 * i]) > 200)
77 cap0 = i;
78 if (ref0 < 0 || cap0 < 0) {
79 printf("wav: %ld frames (%ld ms), no chirp found (silent)\n", frames, frames / 48);
80 free(ref);
81 return 1;
82 }
83 /* best alignment within +-64 frames of the threshold crossing */
84 long len = (long)CK_CHIRP_FRAMES - ref0, best_off = 0;
85 double best = -2;
86 for (long off = -64; off <= 64; off++) {
87 double sxy = 0, sxx = 0, syy = 0;
88 for (long i = 0; i < len; i++) {
89 long c = cap0 + off + i, r = ref0 + i;
90 if (c < 0 || c >= frames)
91 continue;
92 double x = s[2 * c], y = ref[2 * r];
93 sxy += x * y, sxx += x * x, syy += y * y;
94 }
95 double corr = sxx > 0 && syy > 0 ? sxy / sqrt(sxx * syy) : -1;
96 if (corr > best)
97 best = corr, best_off = off;
98 }
99 long maxd = 0, covered = 0;
100 for (long i = 0; i < len; i++) {
101 long c = cap0 + best_off + i;
102 if (c < 0 || c >= frames)
103 continue;
104 covered++;
105 long d = labs((long)s[2 * c] - ref[2 * (ref0 + i)]);
106 if (d > maxd)
107 maxd = d;
108 }
109 long start = cap0 + best_off - ref0; /* capture frame of the night's 0 ms */
110 printf("wav: %ld frames (%ld ms) 2 ch 48000 Hz; chirp at capture %ld ms, %ld of %ld chirp frames present, "
111 "correlation %.4f, largest sample difference %ld\n",
112 frames, frames / 48, (cap0 + best_off) / 48, covered, len, best, maxd);
113 (void)start;
114 free(ref);
115 free(b);
116 return best >= 0.99 && covered == len ? 0 : 1;
117}
118
119static int ppm(const char *path)
120{
121 long n;
122 unsigned char *b = slurp(path, &n);
123 unsigned w = 0, h = 0, mx = 0;
124 int off = 0;
125 if (!b || sscanf((char *)b, "P6 %u %u %u%n", &w, &h, &mx, &off) != 3 || mx != 255 ||
126 (long)off + 1 + (long)w * h * 3 > n) {
127 printf("ppm: %s unreadable\n", path);
128 return 1;
129 }
130 const unsigned char *p = b + off + 1;
131 unsigned long lit = 0, peak = 0;
132 for (unsigned long i = 0; i < (unsigned long)w * h; i++) {
133 unsigned l = (p[3 * i] * 2u + p[3 * i + 1] * 5u + p[3 * i + 2]) / 8u;
134 if (l > 64)
135 lit++;
136 if (l > peak)
137 peak = l;
138 }
139 double share = 100.0 * (double)lit / ((double)w * h);
140 printf("ppm: %ux%u, %.3f%% of pixels lit (above 64 of 255), brightest %lu\n", w, h, share, peak);
141 free(b);
142 return share > 0.05 ? 0 : 1;
143}
144
145int main(int argc, char **argv)
146{
147 if (argc == 3 && !strcmp(argv[1], "wav"))
148 return wav(argv[2]);
149 if (argc == 3 && !strcmp(argv[1], "ppm"))
150 return ppm(argv[2]);
151 fprintf(stderr, "usage: boot_show_check wav|ppm FILE\n");
152 return 2;
153}