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 * 9*29d12216SThomas Huth * And based on the network loading code from SLOF (netload.c) 10*29d12216SThomas Huth * Copyright (c) 2004, 2008 IBM Corporation 11*29d12216SThomas 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> 24*29d12216SThomas Huth 25*29d12216SThomas Huth #include <tftp.h> 26*29d12216SThomas Huth #include <ethernet.h> 27*29d12216SThomas Huth #include <dhcp.h> 28*29d12216SThomas Huth #include <dhcpv6.h> 29*29d12216SThomas Huth #include <ipv4.h> 30*29d12216SThomas Huth #include <ipv6.h> 31*29d12216SThomas Huth #include <dns.h> 323e4415a7SThomas Huth #include <time.h> 333e4415a7SThomas Huth 343e4415a7SThomas Huth #include "s390-ccw.h" 353e4415a7SThomas Huth #include "virtio.h" 363e4415a7SThomas Huth 37*29d12216SThomas Huth #define DEFAULT_BOOT_RETRIES 10 38*29d12216SThomas Huth #define DEFAULT_TFTP_RETRIES 20 39*29d12216SThomas Huth 403e4415a7SThomas Huth extern char _start[]; 413e4415a7SThomas Huth 423e4415a7SThomas Huth char stack[PAGE_SIZE * 8] __attribute__((aligned(PAGE_SIZE))); 433e4415a7SThomas Huth IplParameterBlock iplb __attribute__((aligned(PAGE_SIZE))); 443e4415a7SThomas Huth 453e4415a7SThomas Huth static SubChannelId net_schid = { .one = 1 }; 46*29d12216SThomas Huth static int ip_version = 4; 473e4415a7SThomas Huth static uint64_t dest_timer; 483e4415a7SThomas Huth 493e4415a7SThomas Huth static uint64_t get_timer_ms(void) 503e4415a7SThomas Huth { 513e4415a7SThomas Huth uint64_t clk; 523e4415a7SThomas Huth 533e4415a7SThomas Huth asm volatile(" stck %0 " : : "Q"(clk) : "memory"); 543e4415a7SThomas Huth 553e4415a7SThomas Huth /* Bit 51 is incremented each microsecond */ 563e4415a7SThomas Huth return (clk >> (63 - 51)) / 1000; 573e4415a7SThomas Huth } 583e4415a7SThomas Huth 593e4415a7SThomas Huth void set_timer(int val) 603e4415a7SThomas Huth { 613e4415a7SThomas Huth dest_timer = get_timer_ms() + val; 623e4415a7SThomas Huth } 633e4415a7SThomas Huth 643e4415a7SThomas Huth int get_timer(void) 653e4415a7SThomas Huth { 663e4415a7SThomas Huth return dest_timer - get_timer_ms(); 673e4415a7SThomas Huth } 683e4415a7SThomas Huth 693e4415a7SThomas Huth int get_sec_ticks(void) 703e4415a7SThomas Huth { 713e4415a7SThomas Huth return 1000; /* number of ticks in 1 second */ 723e4415a7SThomas Huth } 733e4415a7SThomas Huth 74*29d12216SThomas Huth /** 75*29d12216SThomas Huth * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6). 76*29d12216SThomas Huth * @param fn_ip contains the following configuration information: 77*29d12216SThomas Huth * client MAC, client IP, TFTP-server MAC, TFTP-server IP, 78*29d12216SThomas Huth * boot file name 79*29d12216SThomas Huth * @param retries Number of DHCP attempts 80*29d12216SThomas Huth * @return 0 : IP and configuration info obtained; 81*29d12216SThomas Huth * non-0 : error condition occurred. 82*29d12216SThomas Huth */ 83*29d12216SThomas Huth static int dhcp(struct filename_ip *fn_ip, int retries) 84*29d12216SThomas Huth { 85*29d12216SThomas Huth int i = retries + 1; 86*29d12216SThomas Huth int rc = -1; 87*29d12216SThomas Huth 88*29d12216SThomas Huth printf(" Requesting information via DHCP: "); 89*29d12216SThomas Huth 90*29d12216SThomas Huth dhcpv4_generate_transaction_id(); 91*29d12216SThomas Huth dhcpv6_generate_transaction_id(); 92*29d12216SThomas Huth 93*29d12216SThomas Huth do { 94*29d12216SThomas Huth printf("\b\b\b%03d", i - 1); 95*29d12216SThomas Huth if (!--i) { 96*29d12216SThomas Huth printf("\nGiving up after %d DHCP requests\n", retries); 97*29d12216SThomas Huth return -1; 98*29d12216SThomas Huth } 99*29d12216SThomas Huth ip_version = 4; 100*29d12216SThomas Huth rc = dhcpv4(NULL, fn_ip); 101*29d12216SThomas Huth if (rc == -1) { 102*29d12216SThomas Huth ip_version = 6; 103*29d12216SThomas Huth set_ipv6_address(fn_ip->fd, 0); 104*29d12216SThomas Huth rc = dhcpv6(NULL, fn_ip); 105*29d12216SThomas Huth if (rc == 0) { 106*29d12216SThomas Huth memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16); 107*29d12216SThomas Huth break; 108*29d12216SThomas Huth } 109*29d12216SThomas Huth } 110*29d12216SThomas Huth if (rc != -1) { /* either success or non-dhcp failure */ 111*29d12216SThomas Huth break; 112*29d12216SThomas Huth } 113*29d12216SThomas Huth } while (1); 114*29d12216SThomas Huth printf("\b\b\b\bdone\n"); 115*29d12216SThomas Huth 116*29d12216SThomas Huth return rc; 117*29d12216SThomas Huth } 118*29d12216SThomas Huth 119*29d12216SThomas Huth /** 120*29d12216SThomas Huth * Seed the random number generator with our mac and current timestamp 121*29d12216SThomas Huth */ 122*29d12216SThomas Huth static void seed_rng(uint8_t mac[]) 123*29d12216SThomas Huth { 124*29d12216SThomas Huth uint64_t seed; 125*29d12216SThomas Huth 126*29d12216SThomas Huth asm volatile(" stck %0 " : : "Q"(seed) : "memory"); 127*29d12216SThomas Huth seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5]; 128*29d12216SThomas Huth srand(seed); 129*29d12216SThomas Huth } 130*29d12216SThomas Huth 131*29d12216SThomas Huth static int tftp_load(filename_ip_t *fnip, void *buffer, int len, 132*29d12216SThomas Huth unsigned int retries, int ip_vers) 133*29d12216SThomas Huth { 134*29d12216SThomas Huth tftp_err_t tftp_err; 135*29d12216SThomas Huth int rc; 136*29d12216SThomas Huth 137*29d12216SThomas Huth rc = tftp(fnip, buffer, len, retries, &tftp_err, 1, 1428, ip_vers); 138*29d12216SThomas Huth 139*29d12216SThomas Huth if (rc > 0) { 140*29d12216SThomas Huth printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, 141*29d12216SThomas Huth rc / 1024); 142*29d12216SThomas Huth } else if (rc == -1) { 143*29d12216SThomas Huth puts("unknown TFTP error"); 144*29d12216SThomas Huth } else if (rc == -2) { 145*29d12216SThomas Huth printf("TFTP buffer of %d bytes is too small for %s\n", 146*29d12216SThomas Huth len, fnip->filename); 147*29d12216SThomas Huth } else if (rc == -3) { 148*29d12216SThomas Huth printf("file not found: %s\n", fnip->filename); 149*29d12216SThomas Huth } else if (rc == -4) { 150*29d12216SThomas Huth puts("TFTP access violation"); 151*29d12216SThomas Huth } else if (rc == -5) { 152*29d12216SThomas Huth puts("illegal TFTP operation"); 153*29d12216SThomas Huth } else if (rc == -6) { 154*29d12216SThomas Huth puts("unknown TFTP transfer ID"); 155*29d12216SThomas Huth } else if (rc == -7) { 156*29d12216SThomas Huth puts("no such TFTP user"); 157*29d12216SThomas Huth } else if (rc == -8) { 158*29d12216SThomas Huth puts("TFTP blocksize negotiation failed"); 159*29d12216SThomas Huth } else if (rc == -9) { 160*29d12216SThomas Huth puts("file exceeds maximum TFTP transfer size"); 161*29d12216SThomas Huth } else if (rc <= -10 && rc >= -15) { 162*29d12216SThomas Huth const char *icmp_err_str; 163*29d12216SThomas Huth switch (rc) { 164*29d12216SThomas Huth case -ICMP_NET_UNREACHABLE - 10: 165*29d12216SThomas Huth icmp_err_str = "net unreachable"; 166*29d12216SThomas Huth break; 167*29d12216SThomas Huth case -ICMP_HOST_UNREACHABLE - 10: 168*29d12216SThomas Huth icmp_err_str = "host unreachable"; 169*29d12216SThomas Huth break; 170*29d12216SThomas Huth case -ICMP_PROTOCOL_UNREACHABLE - 10: 171*29d12216SThomas Huth icmp_err_str = "protocol unreachable"; 172*29d12216SThomas Huth break; 173*29d12216SThomas Huth case -ICMP_PORT_UNREACHABLE - 10: 174*29d12216SThomas Huth icmp_err_str = "port unreachable"; 175*29d12216SThomas Huth break; 176*29d12216SThomas Huth case -ICMP_FRAGMENTATION_NEEDED - 10: 177*29d12216SThomas Huth icmp_err_str = "fragmentation needed and DF set"; 178*29d12216SThomas Huth break; 179*29d12216SThomas Huth case -ICMP_SOURCE_ROUTE_FAILED - 10: 180*29d12216SThomas Huth icmp_err_str = "source route failed"; 181*29d12216SThomas Huth break; 182*29d12216SThomas Huth default: 183*29d12216SThomas Huth icmp_err_str = " UNKNOWN"; 184*29d12216SThomas Huth break; 185*29d12216SThomas Huth } 186*29d12216SThomas Huth printf("ICMP ERROR \"%s\"\n", icmp_err_str); 187*29d12216SThomas Huth } else if (rc == -40) { 188*29d12216SThomas Huth printf("TFTP error occurred after %d bad packets received", 189*29d12216SThomas Huth tftp_err.bad_tftp_packets); 190*29d12216SThomas Huth } else if (rc == -41) { 191*29d12216SThomas Huth printf("TFTP error occurred after missing %d responses", 192*29d12216SThomas Huth tftp_err.no_packets); 193*29d12216SThomas Huth } else if (rc == -42) { 194*29d12216SThomas Huth printf("TFTP error missing block %d, expected block was %d", 195*29d12216SThomas Huth tftp_err.blocks_missed, 196*29d12216SThomas Huth tftp_err.blocks_received); 197*29d12216SThomas Huth } 198*29d12216SThomas Huth 199*29d12216SThomas Huth return rc; 200*29d12216SThomas Huth } 201*29d12216SThomas Huth 202*29d12216SThomas Huth static int net_load(char *buffer, int len) 203*29d12216SThomas Huth { 204*29d12216SThomas Huth filename_ip_t fn_ip; 205*29d12216SThomas Huth uint8_t mac[6]; 206*29d12216SThomas Huth int rc; 207*29d12216SThomas Huth 208*29d12216SThomas Huth memset(&fn_ip, 0, sizeof(filename_ip_t)); 209*29d12216SThomas Huth 210*29d12216SThomas Huth rc = virtio_net_init(mac); 211*29d12216SThomas Huth if (rc < 0) { 212*29d12216SThomas Huth puts("Could not initialize network device"); 213*29d12216SThomas Huth return -101; 214*29d12216SThomas Huth } 215*29d12216SThomas Huth fn_ip.fd = rc; 216*29d12216SThomas Huth 217*29d12216SThomas Huth printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n", 218*29d12216SThomas Huth mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 219*29d12216SThomas Huth 220*29d12216SThomas Huth set_mac_address(mac); /* init ethernet layer */ 221*29d12216SThomas Huth seed_rng(mac); 222*29d12216SThomas Huth 223*29d12216SThomas Huth rc = dhcp(&fn_ip, DEFAULT_BOOT_RETRIES); 224*29d12216SThomas Huth if (rc >= 0) { 225*29d12216SThomas Huth if (ip_version == 4) { 226*29d12216SThomas Huth set_ipv4_address(fn_ip.own_ip); 227*29d12216SThomas Huth } 228*29d12216SThomas Huth } else { 229*29d12216SThomas Huth puts("Could not get IP address"); 230*29d12216SThomas Huth return -101; 231*29d12216SThomas Huth } 232*29d12216SThomas Huth 233*29d12216SThomas Huth if (ip_version == 4) { 234*29d12216SThomas Huth printf(" Using IPv4 address: %d.%d.%d.%d\n", 235*29d12216SThomas Huth (fn_ip.own_ip >> 24) & 0xFF, (fn_ip.own_ip >> 16) & 0xFF, 236*29d12216SThomas Huth (fn_ip.own_ip >> 8) & 0xFF, fn_ip.own_ip & 0xFF); 237*29d12216SThomas Huth } else if (ip_version == 6) { 238*29d12216SThomas Huth char ip6_str[40]; 239*29d12216SThomas Huth ipv6_to_str(fn_ip.own_ip6.addr, ip6_str); 240*29d12216SThomas Huth printf(" Using IPv6 address: %s\n", ip6_str); 241*29d12216SThomas Huth } 242*29d12216SThomas Huth 243*29d12216SThomas Huth if (rc == -2) { 244*29d12216SThomas Huth printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n", 245*29d12216SThomas Huth (fn_ip.server_ip >> 24) & 0xFF, (fn_ip.server_ip >> 16) & 0xFF, 246*29d12216SThomas Huth (fn_ip.server_ip >> 8) & 0xFF, fn_ip.server_ip & 0xFF); 247*29d12216SThomas Huth return -102; 248*29d12216SThomas Huth } 249*29d12216SThomas Huth if (rc == -4 || rc == -3) { 250*29d12216SThomas Huth puts("Can't obtain TFTP server IP address"); 251*29d12216SThomas Huth return -107; 252*29d12216SThomas Huth } 253*29d12216SThomas Huth 254*29d12216SThomas Huth if (ip_version == 4) { 255*29d12216SThomas Huth printf(" Requesting file \"%s\" via TFTP from %d.%d.%d.%d\n", 256*29d12216SThomas Huth fn_ip.filename, 257*29d12216SThomas Huth (fn_ip.server_ip >> 24) & 0xFF, (fn_ip.server_ip >> 16) & 0xFF, 258*29d12216SThomas Huth (fn_ip.server_ip >> 8) & 0xFF, fn_ip.server_ip & 0xFF); 259*29d12216SThomas Huth } else if (ip_version == 6) { 260*29d12216SThomas Huth char ip6_str[40]; 261*29d12216SThomas Huth printf(" Requesting file \"%s\" via TFTP from ", fn_ip.filename); 262*29d12216SThomas Huth ipv6_to_str(fn_ip.server_ip6.addr, ip6_str); 263*29d12216SThomas Huth printf("%s\n", ip6_str); 264*29d12216SThomas Huth } 265*29d12216SThomas Huth 266*29d12216SThomas Huth /* Do the TFTP load and print error message if necessary */ 267*29d12216SThomas Huth rc = tftp_load(&fn_ip, buffer, len, DEFAULT_TFTP_RETRIES, ip_version); 268*29d12216SThomas Huth 269*29d12216SThomas Huth if (ip_version == 4) { 270*29d12216SThomas Huth dhcp_send_release(fn_ip.fd); 271*29d12216SThomas Huth } 272*29d12216SThomas Huth 273*29d12216SThomas Huth return rc; 274*29d12216SThomas Huth } 275*29d12216SThomas Huth 2763e4415a7SThomas Huth void panic(const char *string) 2773e4415a7SThomas Huth { 2783e4415a7SThomas Huth sclp_print(string); 2793e4415a7SThomas Huth for (;;) { 2803e4415a7SThomas Huth disabled_wait(); 2813e4415a7SThomas Huth } 2823e4415a7SThomas Huth } 2833e4415a7SThomas Huth 2843e4415a7SThomas Huth static bool find_net_dev(Schib *schib, int dev_no) 2853e4415a7SThomas Huth { 2863e4415a7SThomas Huth int i, r; 2873e4415a7SThomas Huth 2883e4415a7SThomas Huth for (i = 0; i < 0x10000; i++) { 2893e4415a7SThomas Huth net_schid.sch_no = i; 2903e4415a7SThomas Huth r = stsch_err(net_schid, schib); 2913e4415a7SThomas Huth if (r == 3 || r == -EIO) { 2923e4415a7SThomas Huth break; 2933e4415a7SThomas Huth } 2943e4415a7SThomas Huth if (!schib->pmcw.dnv) { 2953e4415a7SThomas Huth continue; 2963e4415a7SThomas Huth } 2973e4415a7SThomas Huth if (!virtio_is_supported(net_schid)) { 2983e4415a7SThomas Huth continue; 2993e4415a7SThomas Huth } 3003e4415a7SThomas Huth if (virtio_get_device_type() != VIRTIO_ID_NET) { 3013e4415a7SThomas Huth continue; 3023e4415a7SThomas Huth } 3033e4415a7SThomas Huth if (dev_no < 0 || schib->pmcw.dev == dev_no) { 3043e4415a7SThomas Huth return true; 3053e4415a7SThomas Huth } 3063e4415a7SThomas Huth } 3073e4415a7SThomas Huth 3083e4415a7SThomas Huth return false; 3093e4415a7SThomas Huth } 3103e4415a7SThomas Huth 3113e4415a7SThomas Huth static void virtio_setup(void) 3123e4415a7SThomas Huth { 3133e4415a7SThomas Huth Schib schib; 3143e4415a7SThomas Huth int ssid; 3153e4415a7SThomas Huth bool found = false; 3163e4415a7SThomas Huth uint16_t dev_no; 3173e4415a7SThomas Huth 3183e4415a7SThomas Huth /* 3193e4415a7SThomas Huth * We unconditionally enable mss support. In every sane configuration, 3203e4415a7SThomas Huth * this will succeed; and even if it doesn't, stsch_err() can deal 3213e4415a7SThomas Huth * with the consequences. 3223e4415a7SThomas Huth */ 3233e4415a7SThomas Huth enable_mss_facility(); 3243e4415a7SThomas Huth 3253e4415a7SThomas Huth if (store_iplb(&iplb)) { 3263e4415a7SThomas Huth IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected"); 3273e4415a7SThomas Huth dev_no = iplb.ccw.devno; 3283e4415a7SThomas Huth debug_print_int("device no. ", dev_no); 3293e4415a7SThomas Huth net_schid.ssid = iplb.ccw.ssid & 0x3; 3303e4415a7SThomas Huth debug_print_int("ssid ", net_schid.ssid); 3313e4415a7SThomas Huth found = find_net_dev(&schib, dev_no); 3323e4415a7SThomas Huth } else { 3333e4415a7SThomas Huth for (ssid = 0; ssid < 0x3; ssid++) { 3343e4415a7SThomas Huth net_schid.ssid = ssid; 3353e4415a7SThomas Huth found = find_net_dev(&schib, -1); 3363e4415a7SThomas Huth if (found) { 3373e4415a7SThomas Huth break; 3383e4415a7SThomas Huth } 3393e4415a7SThomas Huth } 3403e4415a7SThomas Huth } 3413e4415a7SThomas Huth 3423e4415a7SThomas Huth IPL_assert(found, "No virtio net device found"); 3433e4415a7SThomas Huth } 3443e4415a7SThomas Huth 3453e4415a7SThomas Huth void main(void) 3463e4415a7SThomas Huth { 347*29d12216SThomas Huth int rc; 348*29d12216SThomas Huth 3493e4415a7SThomas Huth sclp_setup(); 3503e4415a7SThomas Huth sclp_print("Network boot starting...\n"); 3513e4415a7SThomas Huth 3523e4415a7SThomas Huth virtio_setup(); 3533e4415a7SThomas Huth 354*29d12216SThomas Huth rc = net_load(NULL, (long)_start); 355*29d12216SThomas Huth if (rc > 0) { 356*29d12216SThomas Huth sclp_print("Network loading done, starting kernel...\n"); 357*29d12216SThomas Huth asm volatile (" lpsw 0(%0) " : : "r"(0) : "memory"); 358*29d12216SThomas Huth } 359*29d12216SThomas Huth 3603e4415a7SThomas Huth panic("Failed to load OS from network\n"); 3613e4415a7SThomas Huth } 362