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