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/x86/setup_x86.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 / x86 / setup_x86.c` 258 lines ยท 9081 bytes Raw Blame History 1 /* setup_x86.c -- Allen's setup wizard (svc/setup_wizard.h) in his window on 2 * the x86 kernel: 3 * 4 * 1. what this computer is: in the launcher image (CK_X86_HOSTDESC=1) the 5 * host description a launcher hands over as a QEMU fw_cfg file 6 * (svc/setup_inv.h), then in every image what this kernel finds itself 7 * (display controllers on PCI, the screen mode the firmware set, the 8 * processor's name and the cores that started, the RAM in the memory 9 * map, the SATA disks); 10 * 2. the saved record on Allen's disk section (svc/setup_store.h): a 11 * finished setup does not start again; an unfinished one carries on at 12 * its step. Saved after every step; 13 * 3. keys come here before the line editor while the wizard is up, so 14 * nothing typed into it is echoed or logged (the key above all); 15 * 4. API keys are never submitted to the unencrypted courier. A requested 16 * key test fails locally and erases the key; configure credentials on 17 * the host instead. The discovered local model needs no key here. 18 * 19 * The console (COM1) gets "wizard: " lines only (sw_event: steps and 20 * choices, never what was typed). */ 21 #include "../core/ck_internal.h" 22 #include "../svc/courier.h" 23 #include "usb_hid.h" 24 #include "../svc/setup_inv.h" 25 #include "../svc/setup_store.h" 26 #include "../svc/setup_wizard.h" 27 #include "../ui/setup_view.h" 28 #include "x86.h" 29 30 static si_inv inv; 31 static sw_wizard wiz; 32 static ck_setup_store store; 33 static const disk_dev *part; 34 static const ck_courier *wire; 35 static uint64_t hz, t0; 36 static int on; 37 static int pending; /* open, waiting for Allen to form before the first word */ 38 static unsigned saved_n; /* the saved record to carry on from (0: a first start) */ 39 static unsigned said = ~0u, gen = ~0u; 40 static char text[CK_SETUP_TEXT + 1]; 41 #if CK_X86_HOSTDESC 42 static char host[4096]; 43 #endif 44 45 static uint32_t now_ms(void) { return (uint32_t)((ck_rdtsc() - t0) * 1000u / hz); } 46 47 /* The processor's own name (CPUID 0x80000002..4), leading spaces dropped. */ 48 static void cpu_name(char out[49]) 49 { 50 uint32_t r[4]; 51 out[0] = 0; 52 ck_cpuid(0x80000000u, 0, r); 53 if (r[0] < 0x80000004u) 54 return; 55 for (uint32_t l = 0; l < 3; l++) { 56 ck_cpuid(0x80000002u + l, 0, r); 57 memcpy(out + 16 * l, r, 16); 58 } 59 out[48] = 0; 60 unsigned s = 0; 61 while (out[s] == ' ') 62 s++; 63 for (unsigned i = 0; s && i + s < 49; i++) 64 out[i] = out[i + s]; 65 } 66 67 static void gather(const struct ck_x86_setup_facts *f) 68 { 69 si_init(&inv); 70 #if CK_X86_HOSTDESC 71 unsigned n = 0; 72 ck_puts("wizard: launcher image: the setup wizard reads the host description (QEMU fw_cfg, read-only)\n"); 73 int rc = ck_x86_fwcfg_file(f->rsdp, "opt/aienos/host", host, sizeof host, &n); 74 if (rc >= 0) { 75 unsigned items = si_parse(&inv, host, n); 76 ck_printf("wizard: host description %u bytes%s, %u items, %u not understood\n", n, rc ? " (cut)" : "", items, 77 inv.skipped); 78 } else { 79 ck_puts("wizard: no host description; only what this computer says about itself\n"); 80 } 81 #else 82 ck_puts("wizard: only what this computer says about itself\n"); 83 #endif 84 for (unsigned i = 0;; i++) { 85 uint16_t vendor, device; 86 uint32_t cls; 87 if (ck_x86_pci_fn(i, &vendor, &device, &cls)) 88 break; 89 inv.npci++; 90 if ((cls >> 16) == 0x03) 91 si_add_gpu(&inv, vendor, device, cls); 92 } 93 if (!inv.ndisplay && f->screen_w && f->screen_h) 94 si_add_display(&inv, (uint16_t)f->screen_w, (uint16_t)f->screen_h); 95 char cpu[49]; 96 cpu_name(cpu); 97 if (f->cores) 98 si_add_cpus(&inv, f->cores, cpu[0] ? cpu : "this processor"); 99 si_add_ram(&inv, f->ram_bytes); 100 for (unsigned i = 0;; i++) { 101 const char *model = 0; 102 const disk_dev *d = ck_x86_ahci_dev(i, &model); 103 if (!d) 104 break; 105 si_add_disk(&inv, model && model[0] ? model : "a disk", d->block_count * d->block_size, SI_DISK_UNKNOWN); 106 } 107 ck_printf("wizard: found %u graphics, %u screen%s, %u PCI functions, %u disk%s, %u model%s, %u nearby\n", inv.ngpu, 108 inv.ndisplay, inv.ndisplay == 1 ? "" : "s", inv.npci, inv.ndisk, inv.ndisk == 1 ? "" : "s", inv.nmodel, 109 inv.nmodel == 1 ? "" : "s", inv.npeer); 110 } 111 112 static void events(void) 113 { 114 char ev[SW_LINE]; 115 while (sw_event(&wiz, ev, sizeof ev)) 116 ck_printf("wizard: %s\n", ev); 117 } 118 119 /* The step, then llm.conf and the consent record, in one copy (setup_store.h). */ 120 static void save(void) 121 { 122 if (!part) 123 return; 124 unsigned n = sw_record(&wiz, text, CK_SETUP_TEXT + 1); 125 if (!n) { 126 ck_puts("wizard: the record did not fit; not saved\n"); 127 return; 128 } 129 int rc = ck_setup_save(&store, text, n); 130 if (rc) { 131 ck_printf("wizard: saving the step FAILED (rc=%d); setup carries on without it\n", rc); 132 part = 0; 133 return; 134 } 135 ck_printf("wizard: saved at %s (generation %llu, %u bytes)\n", sw_step_name(sw_step_of(&wiz)), 136 (unsigned long long)store.gen, n); 137 } 138 139 static void show(void) 140 { 141 const sw_view *v = sw_view_of(&wiz); 142 int done = sw_step_of(&wiz) == SW_DONE; 143 if (v->gen == gen && !done) 144 return; 145 gen = v->gen; 146 ck_setup_show(v, &said, done, ck_rdtsc()); 147 if (done) { 148 on = 0; 149 ck_puts("wizard: finished; Allen's window is his own again\n"); 150 } 151 } 152 153 /* The first screen once Allen can speak: its timers (hello moves on after 6 s) 154 * count from his first word, not from the window opening. */ 155 static void begin(void) 156 { 157 if (!pending || !ck_allen_ui_formed(ck_rdtsc())) 158 return; 159 pending = 0; 160 t0 = ck_rdtsc(); 161 /* courier 0: this build sends no key test, so the wizard says so at the 162 * pick and never opens the key screen to type a secret that cannot be used */ 163 sw_start(&wiz, &inv, 0, saved_n ? text : 0, saved_n, now_ms()); 164 said = gen = ~0u; 165 events(); 166 show(); 167 } 168 169 int ck_x86_setup_open(const struct ck_x86_setup_facts *f, const struct ck_courier *c, uint64_t tsc_hz, int again) 170 { 171 unsigned n = 0; 172 int rc = 1; 173 hz = tsc_hz; 174 t0 = ck_rdtsc(); 175 wire = c; 176 part = ck_x86_memory_part(); 177 if (part) { 178 rc = ck_setup_load(&store, part, text, sizeof text, &n); 179 if (rc < 0) { 180 ck_printf("wizard: the saved record could not be read (rc=%d); setup starts fresh and is not kept\n", rc); 181 part = 0; 182 } 183 } else { 184 ck_puts("wizard: no room on Allen's disk section this start; setup is not kept\n"); 185 } 186 if (!part && !again) { /* it could not remember being finished: it would come back every start */ 187 ck_puts("wizard: not shown, nowhere to keep it this start (\"set up again\" opens it)\n"); 188 return 0; 189 } 190 /* By itself only at the first start, or to resume a setup saved part way 191 * (a power cut); a later start with nothing saved leaves Allen's window to him. */ 192 if (rc == 1 && !again && f->starts > 1) { 193 ck_printf("wizard: not shown, start %u and nothing half done (\"set up again\" opens it)\n", 194 (unsigned)f->starts); 195 return 0; 196 } 197 if (rc == 0 && !again && sw_saved_done(text, n)) { 198 ck_printf("wizard: setup finished on an earlier start (generation %llu); not shown\n", 199 (unsigned long long)store.gen); 200 return 0; 201 } 202 gather(f); 203 saved_n = rc == 0 && !again ? n : 0; 204 on = pending = 1; /* keys before Allen has formed are taken and dropped */ 205 begin(); 206 return 1; 207 } 208 209 int ck_x86_setup_on(void) { return on; } 210 211 int ck_x86_setup_key(int kind, char c) 212 { 213 static const uint8_t map[] = { 214 [CK_KEY_CHAR] = SW_K_CHAR, [CK_KEY_ENTER] = SW_K_ENTER, [CK_KEY_BACKSPACE] = SW_K_BACK, 215 [CK_KEY_ESCAPE] = SW_K_ESC, [CK_KEY_UP] = SW_K_UP, [CK_KEY_DOWN] = SW_K_DOWN, 216 [CK_KEY_LEFT] = SW_K_LEFT, [CK_KEY_RIGHT] = SW_K_RIGHT, [CK_KEY_TAB] = SW_K_TAB, 217 }; 218 if (!on) 219 return 0; 220 if (pending) 221 return 1; 222 if (kind <= 0 || (unsigned)kind >= sizeof map || !map[kind]) 223 return 1; /* anything else is taken and dropped while the wizard is up */ 224 int step = sw_step_of(&wiz); 225 sw_key(&wiz, map[kind], c, now_ms()); 226 events(); 227 if (sw_step_of(&wiz) != step) 228 save(); 229 show(); 230 return 1; 231 } 232 233 /* Old courier key-test replies cannot change setup: no key test is sent. */ 234 int ck_x86_setup_line(const char *line) 235 { 236 size_t n = strlen(line); 237 return (n >= 6 && !memcmp(line, "KEYOK ", 6)) || (n >= 7 && !memcmp(line, "KEYBAD ", 7)); 238 } 239 240 void ck_x86_setup_step(void) 241 { 242 if (!on) 243 return; 244 if (pending) { 245 begin(); 246 return; 247 } 248 int step = sw_step_of(&wiz); 249 sw_tick(&wiz, now_ms()); 250 const char *kind, *url, *model, *key; 251 if (sw_test_wanted(&wiz, &kind, &url, &model, &key)) { 252 sw_test_done(&wiz, 0, "Set the service key on the host, then choose its local model here.", now_ms()); 253 } 254 events(); 255 if (sw_step_of(&wiz) != step) 256 save(); 257 show(); 258 }