xref: /qemu/pc-bios/s390-ccw/netmain.c (revision 09be82ce)
13e4415a7SThomas Huth /*
23e4415a7SThomas Huth  * S390 virtio-ccw network boot loading program
33e4415a7SThomas Huth  *
43e4415a7SThomas Huth  * Copyright 2017 Thomas Huth, Red Hat Inc.
53e4415a7SThomas Huth  *
63e4415a7SThomas Huth  * Based on the S390 virtio-ccw loading program (main.c)
73e4415a7SThomas Huth  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
83e4415a7SThomas Huth  *
929d12216SThomas Huth  * And based on the network loading code from SLOF (netload.c)
1029d12216SThomas Huth  * Copyright (c) 2004, 2008 IBM Corporation
1129d12216SThomas Huth  *
123e4415a7SThomas Huth  * This code is free software; you can redistribute it and/or modify it
133e4415a7SThomas Huth  * under the terms of the GNU General Public License as published by the
143e4415a7SThomas Huth  * Free Software Foundation; either version 2 of the License, or (at your
153e4415a7SThomas Huth  * option) any later version.
163e4415a7SThomas Huth  */
173e4415a7SThomas Huth 
183e4415a7SThomas Huth #include <stdint.h>
193e4415a7SThomas Huth #include <stdbool.h>
203e4415a7SThomas Huth #include <stdio.h>
213e4415a7SThomas Huth #include <stdlib.h>
223e4415a7SThomas Huth #include <string.h>
233e4415a7SThomas Huth #include <unistd.h>
2429d12216SThomas Huth 
2529d12216SThomas Huth #include <tftp.h>
2629d12216SThomas Huth #include <ethernet.h>
2729d12216SThomas Huth #include <dhcp.h>
2829d12216SThomas Huth #include <dhcpv6.h>
2929d12216SThomas Huth #include <ipv4.h>
3029d12216SThomas Huth #include <ipv6.h>
3129d12216SThomas Huth #include <dns.h>
323e4415a7SThomas Huth #include <time.h>
33ec623990SThomas Huth #include <pxelinux.h>
343e4415a7SThomas Huth 
353e4415a7SThomas Huth #include "s390-ccw.h"
36120d0410SJason J. Herne #include "cio.h"
373e4415a7SThomas Huth #include "virtio.h"
383e4415a7SThomas Huth 
3929d12216SThomas Huth #define DEFAULT_BOOT_RETRIES 10
4029d12216SThomas Huth #define DEFAULT_TFTP_RETRIES 20
4129d12216SThomas Huth 
423e4415a7SThomas Huth extern char _start[];
433e4415a7SThomas Huth 
44c4942ee9SThomas Huth #define KERNEL_ADDR             ((void *)0L)
45c4942ee9SThomas Huth #define KERNEL_MAX_SIZE         ((long)_start)
46ec623990SThomas Huth #define ARCH_COMMAND_LINE_SIZE  896              /* Taken from Linux kernel */
47c4942ee9SThomas Huth 
480d8261b5SThomas Huth /* STSI 3.2.2 offset of first vmdb + offset of uuid inside vmdb */
490d8261b5SThomas Huth #define STSI322_VMDB_UUID_OFFSET ((8 + 12) * 4)
500d8261b5SThomas Huth 
513e4415a7SThomas Huth char stack[PAGE_SIZE * 8] __attribute__((aligned(PAGE_SIZE)));
523e4415a7SThomas Huth IplParameterBlock iplb __attribute__((aligned(PAGE_SIZE)));
53c4942ee9SThomas Huth static char cfgbuf[2048];
543e4415a7SThomas Huth 
553e4415a7SThomas Huth static SubChannelId net_schid = { .one = 1 };
56ec623990SThomas Huth static uint8_t mac[6];
573e4415a7SThomas Huth static uint64_t dest_timer;
583e4415a7SThomas Huth 
593e4415a7SThomas Huth static uint64_t get_timer_ms(void)
603e4415a7SThomas Huth {
613e4415a7SThomas Huth     uint64_t clk;
623e4415a7SThomas Huth 
633e4415a7SThomas Huth     asm volatile(" stck %0 " : : "Q"(clk) : "memory");
643e4415a7SThomas Huth 
653e4415a7SThomas Huth     /* Bit 51 is incremented each microsecond */
663e4415a7SThomas Huth     return (clk >> (63 - 51)) / 1000;
673e4415a7SThomas Huth }
683e4415a7SThomas Huth 
693e4415a7SThomas Huth void set_timer(int val)
703e4415a7SThomas Huth {
713e4415a7SThomas Huth     dest_timer = get_timer_ms() + val;
723e4415a7SThomas Huth }
733e4415a7SThomas Huth 
743e4415a7SThomas Huth int get_timer(void)
753e4415a7SThomas Huth {
763e4415a7SThomas Huth     return dest_timer - get_timer_ms();
773e4415a7SThomas Huth }
783e4415a7SThomas Huth 
793e4415a7SThomas Huth int get_sec_ticks(void)
803e4415a7SThomas Huth {
813e4415a7SThomas Huth     return 1000;    /* number of ticks in 1 second */
823e4415a7SThomas Huth }
833e4415a7SThomas Huth 
8429d12216SThomas Huth /**
8529d12216SThomas Huth  * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6).
8629d12216SThomas Huth  * @param  fn_ip     contains the following configuration information:
8729d12216SThomas Huth  *                   client MAC, client IP, TFTP-server MAC, TFTP-server IP,
8829d12216SThomas Huth  *                   boot file name
8929d12216SThomas Huth  * @param  retries   Number of DHCP attempts
9029d12216SThomas Huth  * @return           0 : IP and configuration info obtained;
9129d12216SThomas Huth  *                   non-0 : error condition occurred.
9229d12216SThomas Huth  */
9329d12216SThomas Huth static int dhcp(struct filename_ip *fn_ip, int retries)
9429d12216SThomas Huth {
9529d12216SThomas Huth     int i = retries + 1;
9629d12216SThomas Huth     int rc = -1;
9729d12216SThomas Huth 
9829d12216SThomas Huth     printf("  Requesting information via DHCP:     ");
9929d12216SThomas Huth 
10029d12216SThomas Huth     dhcpv4_generate_transaction_id();
10129d12216SThomas Huth     dhcpv6_generate_transaction_id();
10229d12216SThomas Huth 
10329d12216SThomas Huth     do {
10429d12216SThomas Huth         printf("\b\b\b%03d", i - 1);
10529d12216SThomas Huth         if (!--i) {
10629d12216SThomas Huth             printf("\nGiving up after %d DHCP requests\n", retries);
10729d12216SThomas Huth             return -1;
10829d12216SThomas Huth         }
109134f0b3dSThomas Huth         fn_ip->ip_version = 4;
11029d12216SThomas Huth         rc = dhcpv4(NULL, fn_ip);
11129d12216SThomas Huth         if (rc == -1) {
112134f0b3dSThomas Huth             fn_ip->ip_version = 6;
11329d12216SThomas Huth             set_ipv6_address(fn_ip->fd, 0);
11429d12216SThomas Huth             rc = dhcpv6(NULL, fn_ip);
11529d12216SThomas Huth             if (rc == 0) {
11629d12216SThomas Huth                 memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16);
11729d12216SThomas Huth                 break;
11829d12216SThomas Huth             }
11929d12216SThomas Huth         }
12029d12216SThomas Huth         if (rc != -1) {    /* either success or non-dhcp failure */
12129d12216SThomas Huth             break;
12229d12216SThomas Huth         }
12329d12216SThomas Huth     } while (1);
12429d12216SThomas Huth     printf("\b\b\b\bdone\n");
12529d12216SThomas Huth 
12629d12216SThomas Huth     return rc;
12729d12216SThomas Huth }
12829d12216SThomas Huth 
12929d12216SThomas Huth /**
13029d12216SThomas Huth  * Seed the random number generator with our mac and current timestamp
13129d12216SThomas Huth  */
13229d12216SThomas Huth static void seed_rng(uint8_t mac[])
13329d12216SThomas Huth {
13429d12216SThomas Huth     uint64_t seed;
13529d12216SThomas Huth 
13629d12216SThomas Huth     asm volatile(" stck %0 " : : "Q"(seed) : "memory");
13729d12216SThomas Huth     seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
13829d12216SThomas Huth     srand(seed);
13929d12216SThomas Huth }
14029d12216SThomas Huth 
1410c188229SThomas Huth static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
14229d12216SThomas Huth {
14329d12216SThomas Huth     tftp_err_t tftp_err;
14429d12216SThomas Huth     int rc;
14529d12216SThomas Huth 
146134f0b3dSThomas Huth     rc = tftp(fnip, buffer, len, DEFAULT_TFTP_RETRIES, &tftp_err);
14729d12216SThomas Huth 
148c4942ee9SThomas Huth     if (rc < 0) {
149c4942ee9SThomas Huth         /* Make sure that error messages are put into a new line */
150c4942ee9SThomas Huth         printf("\n  ");
151c4942ee9SThomas Huth     }
152c4942ee9SThomas Huth 
153c4942ee9SThomas Huth     if (rc > 1024) {
154c4942ee9SThomas Huth         printf("  TFTP: Received %s (%d KBytes)\n", fnip->filename, rc / 1024);
155c4942ee9SThomas Huth     } else if (rc > 0) {
156c4942ee9SThomas Huth         printf("  TFTP: Received %s (%d Bytes)\n", fnip->filename, rc);
157134f0b3dSThomas Huth     } else {
158134f0b3dSThomas Huth         const char *errstr = NULL;
159134f0b3dSThomas Huth         int ecode;
160134f0b3dSThomas Huth         tftp_get_error_info(fnip, &tftp_err, rc, &errstr, &ecode);
161134f0b3dSThomas Huth         printf("TFTP error: %s\n", errstr ? errstr : "unknown error");
16229d12216SThomas Huth     }
16329d12216SThomas Huth 
16429d12216SThomas Huth     return rc;
16529d12216SThomas Huth }
16629d12216SThomas Huth 
1670c188229SThomas Huth static int net_init(filename_ip_t *fn_ip)
16829d12216SThomas Huth {
16929d12216SThomas Huth     int rc;
17029d12216SThomas Huth 
1710c188229SThomas Huth     memset(fn_ip, 0, sizeof(filename_ip_t));
17229d12216SThomas Huth 
17329d12216SThomas Huth     rc = virtio_net_init(mac);
17429d12216SThomas Huth     if (rc < 0) {
17529d12216SThomas Huth         puts("Could not initialize network device");
17629d12216SThomas Huth         return -101;
17729d12216SThomas Huth     }
1780c188229SThomas Huth     fn_ip->fd = rc;
17929d12216SThomas Huth 
18029d12216SThomas Huth     printf("  Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
18129d12216SThomas Huth            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
18229d12216SThomas Huth 
18329d12216SThomas Huth     set_mac_address(mac);    /* init ethernet layer */
18429d12216SThomas Huth     seed_rng(mac);
18529d12216SThomas Huth 
1860c188229SThomas Huth     rc = dhcp(fn_ip, DEFAULT_BOOT_RETRIES);
18729d12216SThomas Huth     if (rc >= 0) {
188134f0b3dSThomas Huth         if (fn_ip->ip_version == 4) {
1890c188229SThomas Huth             set_ipv4_address(fn_ip->own_ip);
19029d12216SThomas Huth         }
19129d12216SThomas Huth     } else {
19229d12216SThomas Huth         puts("Could not get IP address");
19329d12216SThomas Huth         return -101;
19429d12216SThomas Huth     }
19529d12216SThomas Huth 
196134f0b3dSThomas Huth     if (fn_ip->ip_version == 4) {
19729d12216SThomas Huth         printf("  Using IPv4 address: %d.%d.%d.%d\n",
1980c188229SThomas Huth               (fn_ip->own_ip >> 24) & 0xFF, (fn_ip->own_ip >> 16) & 0xFF,
1990c188229SThomas Huth               (fn_ip->own_ip >>  8) & 0xFF, fn_ip->own_ip & 0xFF);
200134f0b3dSThomas Huth     } else if (fn_ip->ip_version == 6) {
20129d12216SThomas Huth         char ip6_str[40];
2020c188229SThomas Huth         ipv6_to_str(fn_ip->own_ip6.addr, ip6_str);
20329d12216SThomas Huth         printf("  Using IPv6 address: %s\n", ip6_str);
20429d12216SThomas Huth     }
20529d12216SThomas Huth 
20629d12216SThomas Huth     if (rc == -2) {
20729d12216SThomas Huth         printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
2080c188229SThomas Huth                (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
2090c188229SThomas Huth                (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
21029d12216SThomas Huth         return -102;
21129d12216SThomas Huth     }
21229d12216SThomas Huth     if (rc == -4 || rc == -3) {
21329d12216SThomas Huth         puts("Can't obtain TFTP server IP address");
21429d12216SThomas Huth         return -107;
21529d12216SThomas Huth     }
21629d12216SThomas Huth 
2170c188229SThomas Huth     printf("  Using TFTP server: ");
218134f0b3dSThomas Huth     if (fn_ip->ip_version == 4) {
2190c188229SThomas Huth         printf("%d.%d.%d.%d\n",
2200c188229SThomas Huth                (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
2210c188229SThomas Huth                (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
222134f0b3dSThomas Huth     } else if (fn_ip->ip_version == 6) {
22329d12216SThomas Huth         char ip6_str[40];
2240c188229SThomas Huth         ipv6_to_str(fn_ip->server_ip6.addr, ip6_str);
22529d12216SThomas Huth         printf("%s\n", ip6_str);
22629d12216SThomas Huth     }
22729d12216SThomas Huth 
228134f0b3dSThomas Huth     if (strlen(fn_ip->filename) > 0) {
2290c188229SThomas Huth         printf("  Bootfile name: '%s'\n", fn_ip->filename);
23029d12216SThomas Huth     }
23129d12216SThomas Huth 
23229d12216SThomas Huth     return rc;
23329d12216SThomas Huth }
23429d12216SThomas Huth 
2350c188229SThomas Huth static void net_release(filename_ip_t *fn_ip)
2360c188229SThomas Huth {
237134f0b3dSThomas Huth     if (fn_ip->ip_version == 4) {
2380c188229SThomas Huth         dhcp_send_release(fn_ip->fd);
2390c188229SThomas Huth     }
2400c188229SThomas Huth }
2410c188229SThomas Huth 
242c4942ee9SThomas Huth /**
2430d8261b5SThomas Huth  * Retrieve the Universally Unique Identifier of the VM.
2440d8261b5SThomas Huth  * @return UUID string, or NULL in case of errors
2450d8261b5SThomas Huth  */
2460d8261b5SThomas Huth static const char *get_uuid(void)
2470d8261b5SThomas Huth {
2480d8261b5SThomas Huth     register int r0 asm("0");
2490d8261b5SThomas Huth     register int r1 asm("1");
2500d8261b5SThomas Huth     uint8_t *mem, *buf, uuid[16];
2510d8261b5SThomas Huth     int i, cc, chk = 0;
2520d8261b5SThomas Huth     static char uuid_str[37];
2530d8261b5SThomas Huth 
2540d8261b5SThomas Huth     mem = malloc(2 * PAGE_SIZE);
2550d8261b5SThomas Huth     if (!mem) {
2560d8261b5SThomas Huth         puts("Out of memory ... can not get UUID.");
2570d8261b5SThomas Huth         return NULL;
2580d8261b5SThomas Huth     }
2590d8261b5SThomas Huth     buf = (uint8_t *)(((uint64_t)mem + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
2600d8261b5SThomas Huth     memset(buf, 0, PAGE_SIZE);
2610d8261b5SThomas Huth 
2620d8261b5SThomas Huth     /* Get SYSIB 3.2.2 */
2630d8261b5SThomas Huth     r0 = (3 << 28) | 2;
2640d8261b5SThomas Huth     r1 = 2;
2650d8261b5SThomas Huth     asm volatile(" stsi 0(%[addr])\n"
2660d8261b5SThomas Huth                  " ipm  %[cc]\n"
2670d8261b5SThomas Huth                  " srl  %[cc],28\n"
2680d8261b5SThomas Huth                  : [cc] "=d" (cc)
2690d8261b5SThomas Huth                  : "d" (r0), "d" (r1), [addr] "a" (buf)
2700d8261b5SThomas Huth                  : "cc", "memory");
2710d8261b5SThomas Huth     if (cc) {
272*09be82ceSYifan Luo         free(mem);
2730d8261b5SThomas Huth         return NULL;
2740d8261b5SThomas Huth     }
2750d8261b5SThomas Huth 
2760d8261b5SThomas Huth     for (i = 0; i < 16; i++) {
2770d8261b5SThomas Huth         uuid[i] = buf[STSI322_VMDB_UUID_OFFSET + i];
2780d8261b5SThomas Huth         chk |= uuid[i];
2790d8261b5SThomas Huth     }
2800d8261b5SThomas Huth     free(mem);
2810d8261b5SThomas Huth     if (!chk) {
2820d8261b5SThomas Huth         return NULL;
2830d8261b5SThomas Huth     }
2840d8261b5SThomas Huth 
2850d8261b5SThomas Huth     sprintf(uuid_str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
2860d8261b5SThomas Huth             "%02x%02x%02x%02x%02x%02x", uuid[0], uuid[1], uuid[2], uuid[3],
2870d8261b5SThomas Huth             uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9], uuid[10],
2880d8261b5SThomas Huth             uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
2890d8261b5SThomas Huth 
2900d8261b5SThomas Huth     return uuid_str;
2910d8261b5SThomas Huth }
2920d8261b5SThomas Huth 
2930d8261b5SThomas Huth /**
294ec623990SThomas Huth  * Load a kernel with initrd (i.e. with the information that we've got from
295ec623990SThomas Huth  * a pxelinux.cfg config file)
296ec623990SThomas Huth  */
297ec623990SThomas Huth static int load_kernel_with_initrd(filename_ip_t *fn_ip,
298ec623990SThomas Huth                                    struct pl_cfg_entry *entry)
299ec623990SThomas Huth {
300ec623990SThomas Huth     int rc;
301ec623990SThomas Huth 
302ec623990SThomas Huth     printf("Loading pxelinux.cfg entry '%s'\n", entry->label);
303ec623990SThomas Huth 
304ec623990SThomas Huth     if (!entry->kernel) {
305ec623990SThomas Huth         printf("Kernel entry is missing!\n");
306ec623990SThomas Huth         return -1;
307ec623990SThomas Huth     }
308ec623990SThomas Huth 
309ec623990SThomas Huth     strncpy(fn_ip->filename, entry->kernel, sizeof(fn_ip->filename));
310ec623990SThomas Huth     rc = tftp_load(fn_ip, KERNEL_ADDR, KERNEL_MAX_SIZE);
311ec623990SThomas Huth     if (rc < 0) {
312ec623990SThomas Huth         return rc;
313ec623990SThomas Huth     }
314ec623990SThomas Huth 
315ec623990SThomas Huth     if (entry->initrd) {
316ec623990SThomas Huth         uint64_t iaddr = (rc + 0xfff) & ~0xfffUL;
317ec623990SThomas Huth 
318ec623990SThomas Huth         strncpy(fn_ip->filename, entry->initrd, sizeof(fn_ip->filename));
319ec623990SThomas Huth         rc = tftp_load(fn_ip, (void *)iaddr, KERNEL_MAX_SIZE - iaddr);
320ec623990SThomas Huth         if (rc < 0) {
321ec623990SThomas Huth             return rc;
322ec623990SThomas Huth         }
323ec623990SThomas Huth         /* Patch location and size: */
324ec623990SThomas Huth         *(uint64_t *)0x10408 = iaddr;
325ec623990SThomas Huth         *(uint64_t *)0x10410 = rc;
326ec623990SThomas Huth         rc += iaddr;
327ec623990SThomas Huth     }
328ec623990SThomas Huth 
329ec623990SThomas Huth     if (entry->append) {
330ec623990SThomas Huth         strncpy((char *)0x10480, entry->append, ARCH_COMMAND_LINE_SIZE);
331ec623990SThomas Huth     }
332ec623990SThomas Huth 
333ec623990SThomas Huth     return rc;
334ec623990SThomas Huth }
335ec623990SThomas Huth 
336ec623990SThomas Huth #define MAX_PXELINUX_ENTRIES 16
337ec623990SThomas Huth 
338ec623990SThomas Huth static int net_try_pxelinux_cfg(filename_ip_t *fn_ip)
339ec623990SThomas Huth {
340ec623990SThomas Huth     struct pl_cfg_entry entries[MAX_PXELINUX_ENTRIES];
341ec623990SThomas Huth     int num_ent, def_ent = 0;
342ec623990SThomas Huth 
3430d8261b5SThomas Huth     num_ent = pxelinux_load_parse_cfg(fn_ip, mac, get_uuid(),
3440d8261b5SThomas Huth                                       DEFAULT_TFTP_RETRIES,
345ec623990SThomas Huth                                       cfgbuf, sizeof(cfgbuf),
346ec623990SThomas Huth                                       entries, MAX_PXELINUX_ENTRIES, &def_ent);
347ec623990SThomas Huth     if (num_ent > 0) {
348ec623990SThomas Huth         return load_kernel_with_initrd(fn_ip, &entries[def_ent]);
349ec623990SThomas Huth     }
350ec623990SThomas Huth 
351ec623990SThomas Huth     return -1;
352ec623990SThomas Huth }
353ec623990SThomas Huth 
354ec623990SThomas Huth /**
355c4942ee9SThomas Huth  * Load via information from a .INS file (which can be found on CD-ROMs
356c4942ee9SThomas Huth  * for example)
357c4942ee9SThomas Huth  */
358c4942ee9SThomas Huth static int handle_ins_cfg(filename_ip_t *fn_ip, char *cfg, int cfgsize)
359c4942ee9SThomas Huth {
360c4942ee9SThomas Huth     char *ptr;
361c4942ee9SThomas Huth     int rc = -1, llen;
362c4942ee9SThomas Huth     void *destaddr;
363c4942ee9SThomas Huth     char *insbuf = cfg;
364c4942ee9SThomas Huth 
365c4942ee9SThomas Huth     ptr = strchr(insbuf, '\n');
366c4942ee9SThomas Huth     if (!ptr) {
367c4942ee9SThomas Huth         puts("Does not seem to be a valid .INS file");
368c4942ee9SThomas Huth         return -1;
369c4942ee9SThomas Huth     }
370c4942ee9SThomas Huth 
371c4942ee9SThomas Huth     *ptr = 0;
372c4942ee9SThomas Huth     printf("\nParsing .INS file:\n %s\n", &insbuf[2]);
373c4942ee9SThomas Huth 
374c4942ee9SThomas Huth     insbuf = ptr + 1;
375c4942ee9SThomas Huth     while (*insbuf && insbuf < cfg + cfgsize) {
376c4942ee9SThomas Huth         ptr = strchr(insbuf, '\n');
377c4942ee9SThomas Huth         if (ptr) {
378c4942ee9SThomas Huth             *ptr = 0;
379c4942ee9SThomas Huth         }
380c4942ee9SThomas Huth         llen = strlen(insbuf);
381c4942ee9SThomas Huth         if (!llen) {
382c4942ee9SThomas Huth             insbuf = ptr + 1;
383c4942ee9SThomas Huth             continue;
384c4942ee9SThomas Huth         }
385c4942ee9SThomas Huth         ptr = strchr(insbuf, ' ');
386c4942ee9SThomas Huth         if (!ptr) {
387c4942ee9SThomas Huth             puts("Missing space separator in .INS file");
388c4942ee9SThomas Huth             return -1;
389c4942ee9SThomas Huth         }
390c4942ee9SThomas Huth         *ptr = 0;
391134f0b3dSThomas Huth         strncpy(fn_ip->filename, insbuf, sizeof(fn_ip->filename));
392c4942ee9SThomas Huth         destaddr = (char *)atol(ptr + 1);
393c4942ee9SThomas Huth         rc = tftp_load(fn_ip, destaddr, (long)_start - (long)destaddr);
394c4942ee9SThomas Huth         if (rc <= 0) {
395c4942ee9SThomas Huth             break;
396c4942ee9SThomas Huth         }
397c4942ee9SThomas Huth         insbuf += llen + 1;
398c4942ee9SThomas Huth     }
399c4942ee9SThomas Huth 
400c4942ee9SThomas Huth     return rc;
401c4942ee9SThomas Huth }
402c4942ee9SThomas Huth 
403c4942ee9SThomas Huth static int net_try_direct_tftp_load(filename_ip_t *fn_ip)
404c4942ee9SThomas Huth {
405c4942ee9SThomas Huth     int rc;
406c4942ee9SThomas Huth     void *loadaddr = (void *)0x2000;  /* Load right after the low-core */
407c4942ee9SThomas Huth 
408c4942ee9SThomas Huth     rc = tftp_load(fn_ip, loadaddr, KERNEL_MAX_SIZE - (long)loadaddr);
409c4942ee9SThomas Huth     if (rc < 0) {
410c4942ee9SThomas Huth         return rc;
411c4942ee9SThomas Huth     } else if (rc < 8) {
412c4942ee9SThomas Huth         printf("'%s' is too small (%i bytes only).\n", fn_ip->filename, rc);
413c4942ee9SThomas Huth         return -1;
414c4942ee9SThomas Huth     }
415c4942ee9SThomas Huth 
416c4942ee9SThomas Huth     /* Check whether it is a configuration file instead of a kernel */
417c4942ee9SThomas Huth     if (rc < sizeof(cfgbuf) - 1) {
418c4942ee9SThomas Huth         memcpy(cfgbuf, loadaddr, rc);
419c4942ee9SThomas Huth         cfgbuf[rc] = 0;    /* Make sure that it is NUL-terminated */
420c4942ee9SThomas Huth         if (!strncmp("* ", cfgbuf, 2)) {
421c4942ee9SThomas Huth             return handle_ins_cfg(fn_ip, cfgbuf, rc);
422c4942ee9SThomas Huth         }
423ec623990SThomas Huth         /*
424ec623990SThomas Huth          * pxelinux.cfg support via bootfile name is just here for developers'
425ec623990SThomas Huth          * convenience (it eases testing with the built-in DHCP server of QEMU
426ec623990SThomas Huth          * that does not support RFC 5071). The official way to configure a
427ec623990SThomas Huth          * pxelinux.cfg file name is to use DHCP options 209 and 210 instead.
428ec623990SThomas Huth          * So only use the pxelinux.cfg parser here for files that start with
429ec623990SThomas Huth          * a magic comment string.
430ec623990SThomas Huth          */
431ec623990SThomas Huth         if (!strncasecmp("# pxelinux", cfgbuf, 10)) {
432ec623990SThomas Huth             struct pl_cfg_entry entries[MAX_PXELINUX_ENTRIES];
433ec623990SThomas Huth             int num_ent, def_ent = 0;
434ec623990SThomas Huth 
435ec623990SThomas Huth             num_ent = pxelinux_parse_cfg(cfgbuf, sizeof(cfgbuf), entries,
436ec623990SThomas Huth                                          MAX_PXELINUX_ENTRIES, &def_ent);
437ec623990SThomas Huth             if (num_ent <= 0) {
438ec623990SThomas Huth                 return -1;
439ec623990SThomas Huth             }
440ec623990SThomas Huth             return load_kernel_with_initrd(fn_ip, &entries[def_ent]);
441ec623990SThomas Huth         }
442c4942ee9SThomas Huth     }
443c4942ee9SThomas Huth 
444c4942ee9SThomas Huth     /* Move kernel to right location */
445c4942ee9SThomas Huth     memmove(KERNEL_ADDR, loadaddr, rc);
446c4942ee9SThomas Huth 
447c4942ee9SThomas Huth     return rc;
448c4942ee9SThomas Huth }
449c4942ee9SThomas Huth 
4503e4415a7SThomas Huth void panic(const char *string)
4513e4415a7SThomas Huth {
4523e4415a7SThomas Huth     sclp_print(string);
4533e4415a7SThomas Huth     for (;;) {
4543e4415a7SThomas Huth         disabled_wait();
4553e4415a7SThomas Huth     }
4563e4415a7SThomas Huth }
4573e4415a7SThomas Huth 
4589a848adfSThomas Huth void write_subsystem_identification(void)
4599a848adfSThomas Huth {
4609a848adfSThomas Huth     SubChannelId *schid = (SubChannelId *) 184;
4619a848adfSThomas Huth     uint32_t *zeroes = (uint32_t *) 188;
4629a848adfSThomas Huth 
4639a848adfSThomas Huth     *schid = net_schid;
4649a848adfSThomas Huth     *zeroes = 0;
4659a848adfSThomas Huth }
4669a848adfSThomas Huth 
4673e4415a7SThomas Huth static bool find_net_dev(Schib *schib, int dev_no)
4683e4415a7SThomas Huth {
4693e4415a7SThomas Huth     int i, r;
4703e4415a7SThomas Huth 
4713e4415a7SThomas Huth     for (i = 0; i < 0x10000; i++) {
4723e4415a7SThomas Huth         net_schid.sch_no = i;
4733e4415a7SThomas Huth         r = stsch_err(net_schid, schib);
4743e4415a7SThomas Huth         if (r == 3 || r == -EIO) {
4753e4415a7SThomas Huth             break;
4763e4415a7SThomas Huth         }
4773e4415a7SThomas Huth         if (!schib->pmcw.dnv) {
4783e4415a7SThomas Huth             continue;
4793e4415a7SThomas Huth         }
4803668cb7cSJason J. Herne         enable_subchannel(net_schid);
4813e4415a7SThomas Huth         if (!virtio_is_supported(net_schid)) {
4823e4415a7SThomas Huth             continue;
4833e4415a7SThomas Huth         }
4843e4415a7SThomas Huth         if (virtio_get_device_type() != VIRTIO_ID_NET) {
4853e4415a7SThomas Huth             continue;
4863e4415a7SThomas Huth         }
4873e4415a7SThomas Huth         if (dev_no < 0 || schib->pmcw.dev == dev_no) {
4883e4415a7SThomas Huth             return true;
4893e4415a7SThomas Huth         }
4903e4415a7SThomas Huth     }
4913e4415a7SThomas Huth 
4923e4415a7SThomas Huth     return false;
4933e4415a7SThomas Huth }
4943e4415a7SThomas Huth 
4953e4415a7SThomas Huth static void virtio_setup(void)
4963e4415a7SThomas Huth {
4973e4415a7SThomas Huth     Schib schib;
4983e4415a7SThomas Huth     int ssid;
4993e4415a7SThomas Huth     bool found = false;
5003e4415a7SThomas Huth     uint16_t dev_no;
5013e4415a7SThomas Huth 
5023e4415a7SThomas Huth     /*
5033e4415a7SThomas Huth      * We unconditionally enable mss support. In every sane configuration,
5043e4415a7SThomas Huth      * this will succeed; and even if it doesn't, stsch_err() can deal
5053e4415a7SThomas Huth      * with the consequences.
5063e4415a7SThomas Huth      */
5073e4415a7SThomas Huth     enable_mss_facility();
5083e4415a7SThomas Huth 
5093e4415a7SThomas Huth     if (store_iplb(&iplb)) {
5103e4415a7SThomas Huth         IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected");
5113e4415a7SThomas Huth         dev_no = iplb.ccw.devno;
5123e4415a7SThomas Huth         debug_print_int("device no. ", dev_no);
5133e4415a7SThomas Huth         net_schid.ssid = iplb.ccw.ssid & 0x3;
5143e4415a7SThomas Huth         debug_print_int("ssid ", net_schid.ssid);
5153e4415a7SThomas Huth         found = find_net_dev(&schib, dev_no);
5163e4415a7SThomas Huth     } else {
5173e4415a7SThomas Huth         for (ssid = 0; ssid < 0x3; ssid++) {
5183e4415a7SThomas Huth             net_schid.ssid = ssid;
5193e4415a7SThomas Huth             found = find_net_dev(&schib, -1);
5203e4415a7SThomas Huth             if (found) {
5213e4415a7SThomas Huth                 break;
5223e4415a7SThomas Huth             }
5233e4415a7SThomas Huth         }
5243e4415a7SThomas Huth     }
5253e4415a7SThomas Huth 
5263e4415a7SThomas Huth     IPL_assert(found, "No virtio net device found");
5273e4415a7SThomas Huth }
5283e4415a7SThomas Huth 
5293e4415a7SThomas Huth void main(void)
5303e4415a7SThomas Huth {
5310c188229SThomas Huth     filename_ip_t fn_ip;
532c4942ee9SThomas Huth     int rc, fnlen;
53329d12216SThomas Huth 
5343e4415a7SThomas Huth     sclp_setup();
5353e4415a7SThomas Huth     sclp_print("Network boot starting...\n");
5363e4415a7SThomas Huth 
5373e4415a7SThomas Huth     virtio_setup();
5383e4415a7SThomas Huth 
5390c188229SThomas Huth     rc = net_init(&fn_ip);
5400c188229SThomas Huth     if (rc) {
5410c188229SThomas Huth         panic("Network initialization failed. Halting.\n");
5420c188229SThomas Huth     }
5430c188229SThomas Huth 
544134f0b3dSThomas Huth     fnlen = strlen(fn_ip.filename);
545c4942ee9SThomas Huth     if (fnlen > 0 && fn_ip.filename[fnlen - 1] != '/') {
546c4942ee9SThomas Huth         rc = net_try_direct_tftp_load(&fn_ip);
547c4942ee9SThomas Huth     }
548ec623990SThomas Huth     if (rc <= 0) {
549ec623990SThomas Huth         rc = net_try_pxelinux_cfg(&fn_ip);
550ec623990SThomas Huth     }
5510c188229SThomas Huth 
5520c188229SThomas Huth     net_release(&fn_ip);
5530c188229SThomas Huth 
55429d12216SThomas Huth     if (rc > 0) {
55529d12216SThomas Huth         sclp_print("Network loading done, starting kernel...\n");
5569a848adfSThomas Huth         jump_to_low_kernel();
55729d12216SThomas Huth     }
55829d12216SThomas Huth 
5593e4415a7SThomas Huth     panic("Failed to load OS from network\n");
5603e4415a7SThomas Huth }
561