master
c 590 lines 15.5 KB
Raw
1 /*
2 * S390 virtio-ccw network boot loading program
3 *
4 * Copyright 2017 Thomas Huth, Red Hat Inc.
5 *
6 * Based on the S390 virtio-ccw loading program (main.c)
7 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
8 *
9 * And based on the network loading code from SLOF (netload.c)
10 * Copyright (c) 2004, 2008 IBM Corporation
11 *
12 * This code is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <tftp.h>
26 #include <ethernet.h>
27 #include <dhcp.h>
28 #include <dhcpv6.h>
29 #include <ipv4.h>
30 #include <ipv6.h>
31 #include <dns.h>
32 #include <time.h>
33 #include <pxelinux.h>
34
35 #include "s390-ccw.h"
36 #include "cio.h"
37 #include "virtio.h"
38 #include "s390-time.h"
39
40 #define DEFAULT_BOOT_RETRIES 10
41 #define DEFAULT_TFTP_RETRIES 20
42
43 /* Index 0 is reserved for default alias, start PXE cfg indices at 1 */
44 #define PXECFG_MAX (MAX_BOOT_ENTRIES - 1)
45
46 extern char _start[];
47
48 #define KERNEL_ADDR ((void *)0L)
49 #define KERNEL_MAX_SIZE ((long)_start)
50 #define ARCH_COMMAND_LINE_SIZE 896 /* Taken from Linux kernel */
51
52 /* STSI 3.2.2 offset of first vmdb + offset of uuid inside vmdb */
53 #define STSI322_VMDB_UUID_OFFSET ((8 + 12) * 4)
54
55 static char cfgbuf[2048];
56
57 SubChannelId net_schid = { .one = 1 };
58 static uint8_t mac[6];
59 static uint64_t dest_timer;
60
61 void set_timer(int val)
62 {
63 dest_timer = get_time_ms() + val;
64 }
65
66 int get_timer(void)
67 {
68 return dest_timer - get_time_ms();
69 }
70
71 int get_sec_ticks(void)
72 {
73 return 1000; /* number of ticks in 1 second */
74 }
75
76 /**
77 * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6).
78 * @param fn_ip contains the following configuration information:
79 * client MAC, client IP, TFTP-server MAC, TFTP-server IP,
80 * boot file name
81 * @param retries Number of DHCP attempts
82 * @return 0 : IP and configuration info obtained;
83 * non-0 : error condition occurred.
84 */
85 static int dhcp(struct filename_ip *fn_ip, int retries)
86 {
87 int i = retries + 1;
88 int rc = -1;
89
90 printf(" Requesting information via DHCP: ");
91
92 dhcpv4_generate_transaction_id();
93 dhcpv6_generate_transaction_id();
94
95 do {
96 printf("\b\b\b%03d", i - 1);
97 if (!--i) {
98 printf("\nGiving up after %d DHCP requests\n", retries);
99 return -1;
100 }
101 fn_ip->ip_version = 4;
102 rc = dhcpv4(NULL, fn_ip);
103 if (rc == -1) {
104 fn_ip->ip_version = 6;
105 set_ipv6_address(fn_ip->fd, 0);
106 rc = dhcpv6(NULL, fn_ip);
107 if (rc == 0) {
108 memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16);
109 break;
110 }
111 }
112 if (rc != -1) { /* either success or non-dhcp failure */
113 break;
114 }
115 } while (1);
116 printf("\b\b\b\bdone\n");
117
118 return rc;
119 }
120
121 /**
122 * Seed the random number generator with our mac and current timestamp
123 */
124 static void seed_rng(uint8_t mac[])
125 {
126 uint64_t seed;
127
128 asm volatile(" stck %0 " : : "Q"(seed) : "memory");
129 seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
130 srand(seed);
131 }
132
133 static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
134 {
135 tftp_err_t tftp_err;
136 int rc;
137
138 rc = tftp(fnip, buffer, len, DEFAULT_TFTP_RETRIES, &tftp_err);
139
140 if (rc < 0) {
141 /* Make sure that error messages are put into a new line */
142 printf("\n ");
143 }
144
145 if (rc > 1024) {
146 printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, rc / 1024);
147 } else if (rc > 0) {
148 printf(" TFTP: Received %s (%d Bytes)\n", fnip->filename, rc);
149 } else {
150 const char *errstr = NULL;
151 int ecode;
152 tftp_get_error_info(fnip, &tftp_err, rc, &errstr, &ecode);
153 printf("TFTP error: %s\n", errstr ? errstr : "unknown error");
154 }
155
156 return rc;
157 }
158
159 static int net_init_ip(filename_ip_t *fn_ip)
160 {
161 int rc;
162
163 printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
164 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
165
166 set_mac_address(mac); /* init ethernet layer */
167 seed_rng(mac);
168
169 rc = dhcp(fn_ip, DEFAULT_BOOT_RETRIES);
170 if (rc >= 0) {
171 if (fn_ip->ip_version == 4) {
172 set_ipv4_address(fn_ip->own_ip);
173 }
174 } else if (rc == -2) {
175 printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
176 (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
177 (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF);
178 return -102;
179 } else if (rc == -4 || rc == -3) {
180 puts("Can't obtain TFTP server IP address");
181 return -107;
182 } else {
183 puts("Could not get IP address");
184 return -101;
185 }
186
187 if (fn_ip->ip_version == 4) {
188 printf(" Using IPv4 address: %d.%d.%d.%d\n",
189 (fn_ip->own_ip >> 24) & 0xFF, (fn_ip->own_ip >> 16) & 0xFF,
190 (fn_ip->own_ip >> 8) & 0xFF, fn_ip->own_ip & 0xFF);
191 } else if (fn_ip->ip_version == 6) {
192 char ip6_str[40];
193 ipv6_to_str(fn_ip->own_ip6.addr, ip6_str);
194 printf(" Using IPv6 address: %s\n", ip6_str);
195 }
196
197 printf(" Using TFTP server: ");
198 if (fn_ip->ip_version == 4) {
199 printf("%d.%d.%d.%d\n",
200 (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
201 (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF);
202 } else if (fn_ip->ip_version == 6) {
203 char ip6_str[40];
204 ipv6_to_str(fn_ip->server_ip6.addr, ip6_str);
205 printf("%s\n", ip6_str);
206 }
207
208 if (strlen(fn_ip->filename) > 0) {
209 printf(" Bootfile name: '%s'\n", fn_ip->filename);
210 }
211
212 return rc;
213 }
214
215 static int net_init(filename_ip_t *fn_ip)
216 {
217 int rc;
218
219 memset(fn_ip, 0, sizeof(filename_ip_t));
220
221 rc = virtio_net_init(mac);
222 if (rc < 0) {
223 puts("Could not initialize network device");
224 return -101;
225 }
226 fn_ip->fd = rc;
227
228 rc = net_init_ip(fn_ip);
229 if (rc < 0) {
230 virtio_net_deinit();
231 }
232
233 return rc;
234 }
235
236 static void net_release(filename_ip_t *fn_ip)
237 {
238 if (fn_ip->ip_version == 4) {
239 dhcp_send_release(fn_ip->fd);
240 }
241 virtio_net_deinit();
242 }
243
244 /**
245 * Retrieve the Universally Unique Identifier of the VM.
246 * @return UUID string, or NULL in case of errors
247 */
248 static const char *get_uuid(void)
249 {
250 register int r0 asm("0");
251 register int r1 asm("1");
252 uint8_t *mem, *buf, uuid[16];
253 int i, cc, chk = 0;
254 static char uuid_str[37];
255
256 mem = malloc(2 * PAGE_SIZE);
257 if (!mem) {
258 puts("Out of memory ... can not get UUID.");
259 return NULL;
260 }
261 buf = (uint8_t *)(((uint64_t)mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
262 memset(buf, 0, PAGE_SIZE);
263
264 /* Get SYSIB 3.2.2 */
265 r0 = (3 << 28) | 2;
266 r1 = 2;
267 asm volatile(" stsi 0(%[addr])\n"
268 " ipm %[cc]\n"
269 " srl %[cc],28\n"
270 : [cc] "=d" (cc)
271 : "d" (r0), "d" (r1), [addr] "a" (buf)
272 : "cc", "memory");
273 if (cc) {
274 free(mem);
275 return NULL;
276 }
277
278 for (i = 0; i < 16; i++) {
279 uuid[i] = buf[STSI322_VMDB_UUID_OFFSET + i];
280 chk |= uuid[i];
281 }
282 free(mem);
283 if (!chk) {
284 return NULL;
285 }
286
287 sprintf(uuid_str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
288 "%02x%02x%02x%02x%02x%02x", uuid[0], uuid[1], uuid[2], uuid[3],
289 uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9], uuid[10],
290 uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
291
292 return uuid_str;
293 }
294
295 /**
296 * Load a kernel with initrd (i.e. with the information that we've got from
297 * a pxelinux.cfg config file)
298 */
299 static int load_kernel_with_initrd(filename_ip_t *fn_ip,
300 struct pl_cfg_entry *entry)
301 {
302 int rc;
303
304 printf("Loading pxelinux.cfg entry '%s'\n", entry->label);
305
306 if (!entry->kernel) {
307 puts("Kernel entry is missing!\n");
308 return -1;
309 }
310
311 strncpy(fn_ip->filename, entry->kernel, sizeof(fn_ip->filename));
312 rc = tftp_load(fn_ip, KERNEL_ADDR, KERNEL_MAX_SIZE);
313 if (rc < 0) {
314 return rc;
315 }
316
317 if (entry->initrd) {
318 uint64_t iaddr = (rc + 0xfff) & ~0xfffUL;
319
320 strncpy(fn_ip->filename, entry->initrd, sizeof(fn_ip->filename));
321 rc = tftp_load(fn_ip, (void *)iaddr, KERNEL_MAX_SIZE - iaddr);
322 if (rc < 0) {
323 return rc;
324 }
325 /* Patch location and size: */
326 *(uint64_t *)0x10408 = iaddr;
327 *(uint64_t *)0x10410 = rc;
328 rc += iaddr;
329 }
330
331 if (entry->append) {
332 strncpy((char *)0x10480, entry->append, ARCH_COMMAND_LINE_SIZE);
333 }
334
335 return rc;
336 }
337
338 static int net_boot_menu(int num_ent, int def_ent,
339 struct pl_cfg_entry *entries)
340 {
341 bool valid_entries[MAX_BOOT_ENTRIES] = { false };
342 int idx;
343
344 puts("\ns390-ccw pxelinux.cfg boot menu:\n");
345 printf(" [0] default (%d)\n", def_ent + 1);
346 valid_entries[0] = true;
347
348 for (idx = 1; idx <= num_ent; idx++) {
349 printf(" [%d] %s\n", idx, entries[idx - 1].label);
350 valid_entries[idx] = true;
351 }
352 putchar('\n');
353
354 idx = menu_get_boot_index(valid_entries);
355 putchar('\n');
356
357 return idx;
358 }
359
360 static int net_select_and_load_kernel(filename_ip_t *fn_ip,
361 int num_ent, int selected,
362 struct pl_cfg_entry *entries)
363 {
364 unsigned int loadparm = get_loadparm_index();
365
366 if (num_ent <= 0) {
367 return -1;
368 }
369
370 if (menu_is_enabled_enum() && num_ent > 1) {
371 loadparm = net_boot_menu(num_ent, selected, entries);
372 }
373
374 IPL_assert(loadparm <= num_ent,
375 "loadparm is set to an entry that is not available in the "
376 "pxelinux.cfg file!");
377
378 if (loadparm > 0) {
379 selected = loadparm - 1;
380 }
381
382 return load_kernel_with_initrd(fn_ip, &entries[selected]);
383 }
384
385 static int net_try_pxelinux_cfg(filename_ip_t *fn_ip)
386 {
387 struct pl_cfg_entry entries[PXECFG_MAX];
388 int num_ent, def_ent = 0;
389
390 num_ent = pxelinux_load_parse_cfg(fn_ip, mac, get_uuid(),
391 DEFAULT_TFTP_RETRIES,
392 cfgbuf, sizeof(cfgbuf),
393 entries, PXECFG_MAX, &def_ent);
394
395 return net_select_and_load_kernel(fn_ip, num_ent, def_ent, entries);
396 }
397
398 /**
399 * Load via information from a .INS file (which can be found on CD-ROMs
400 * for example)
401 */
402 static int handle_ins_cfg(filename_ip_t *fn_ip, char *cfg, int cfgsize)
403 {
404 char *ptr;
405 int rc = -1, llen;
406 void *destaddr;
407 char *insbuf = cfg;
408
409 ptr = strchr(insbuf, '\n');
410 if (!ptr) {
411 puts("Does not seem to be a valid .INS file");
412 return -1;
413 }
414
415 *ptr = 0;
416 printf("\nParsing .INS file:\n %s\n", &insbuf[2]);
417
418 insbuf = ptr + 1;
419 while (*insbuf && insbuf < cfg + cfgsize) {
420 ptr = strchr(insbuf, '\n');
421 if (ptr) {
422 *ptr = 0;
423 }
424 llen = strlen(insbuf);
425 if (!llen) {
426 insbuf = ptr + 1;
427 continue;
428 }
429 ptr = strchr(insbuf, ' ');
430 if (!ptr) {
431 puts("Missing space separator in .INS file");
432 return -1;
433 }
434 *ptr = 0;
435 strncpy(fn_ip->filename, insbuf, sizeof(fn_ip->filename));
436 destaddr = (char *)atol(ptr + 1);
437 rc = tftp_load(fn_ip, destaddr, (long)_start - (long)destaddr);
438 if (rc <= 0) {
439 break;
440 }
441 insbuf += llen + 1;
442 }
443
444 return rc;
445 }
446
447 static int net_try_direct_tftp_load(filename_ip_t *fn_ip)
448 {
449 int rc;
450 void *loadaddr = (void *)0x2000; /* Load right after the low-core */
451
452 rc = tftp_load(fn_ip, loadaddr, KERNEL_MAX_SIZE - (long)loadaddr);
453 if (rc < 0) {
454 return rc;
455 } else if (rc < 8) {
456 printf("'%s' is too small (%i bytes only).\n", fn_ip->filename, rc);
457 return -1;
458 }
459
460 /* Check whether it is a configuration file instead of a kernel */
461 if (rc < sizeof(cfgbuf) - 1) {
462 memcpy(cfgbuf, loadaddr, rc);
463 cfgbuf[rc] = 0; /* Make sure that it is NUL-terminated */
464 if (!strncmp("* ", cfgbuf, 2)) {
465 return handle_ins_cfg(fn_ip, cfgbuf, rc);
466 }
467 /*
468 * pxelinux.cfg support via bootfile name is just here for developers'
469 * convenience (it eases testing with the built-in DHCP server of QEMU
470 * that does not support RFC 5071). The official way to configure a
471 * pxelinux.cfg file name is to use DHCP options 209 and 210 instead.
472 * So only use the pxelinux.cfg parser here for files that start with
473 * a magic comment string.
474 */
475 if (!strncasecmp("# pxelinux", cfgbuf, 10)) {
476 struct pl_cfg_entry entries[PXECFG_MAX];
477 int num_ent, def_ent = 0;
478
479 num_ent = pxelinux_parse_cfg(cfgbuf, sizeof(cfgbuf), entries,
480 PXECFG_MAX, &def_ent);
481 return net_select_and_load_kernel(fn_ip, num_ent, def_ent,
482 entries);
483 }
484 }
485
486 /* Move kernel to right location */
487 memmove(KERNEL_ADDR, loadaddr, rc);
488
489 return rc;
490 }
491
492 static bool find_net_dev(Schib *schib, int dev_no)
493 {
494 int i, r;
495
496 for (i = 0; i < 0x10000; i++) {
497 net_schid.sch_no = i;
498 r = stsch_err(net_schid, schib);
499 if (r == 3 || r == -EIO) {
500 break;
501 }
502 if (!schib->pmcw.dnv) {
503 continue;
504 }
505 enable_subchannel(net_schid);
506 if (!virtio_is_supported(virtio_get_device())) {
507 continue;
508 }
509 if (virtio_get_device_type() != VIRTIO_ID_NET) {
510 continue;
511 }
512 if (dev_no < 0 || schib->pmcw.dev == dev_no) {
513 return true;
514 }
515 }
516
517 return false;
518 }
519
520 static bool virtio_setup(void)
521 {
522 Schib schib;
523 int ssid;
524 bool found = false;
525 uint16_t dev_no;
526
527 /*
528 * We unconditionally enable mss support. In every sane configuration,
529 * this will succeed; and even if it doesn't, stsch_err() can deal
530 * with the consequences.
531 */
532 enable_mss_facility();
533
534 if (have_iplb || store_iplb(iplb)) {
535 IPL_assert(iplb->pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected");
536 dev_no = iplb->ccw.devno;
537 debug_print_int("device no. ", dev_no);
538 net_schid.ssid = iplb->ccw.ssid & 0x3;
539 debug_print_int("ssid ", net_schid.ssid);
540 found = find_net_dev(&schib, dev_no);
541 } else {
542 for (ssid = 0; ssid < 0x3; ssid++) {
543 net_schid.ssid = ssid;
544 found = find_net_dev(&schib, -1);
545 if (found) {
546 break;
547 }
548 }
549 }
550
551 return found;
552 }
553
554 int netmain(void)
555 {
556 filename_ip_t fn_ip;
557 int rc, fnlen;
558
559 sclp_setup();
560 puts("Network boot starting...");
561
562 if (!virtio_setup()) {
563 puts("No virtio net device found.");
564 return -1;
565 }
566
567 rc = net_init(&fn_ip);
568 if (rc) {
569 puts("Network initialization failed.");
570 return -1;
571 }
572
573 fnlen = strlen(fn_ip.filename);
574 if (fnlen > 0 && fn_ip.filename[fnlen - 1] != '/') {
575 rc = net_try_direct_tftp_load(&fn_ip);
576 }
577 if (rc <= 0) {
578 rc = net_try_pxelinux_cfg(&fn_ip);
579 }
580
581 net_release(&fn_ip);
582
583 if (rc > 0) {
584 puts("Network loading done, starting kernel...");
585 jump_to_low_kernel();
586 }
587
588 puts("Failed to load OS from network.");
589 return -1;
590 }