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> 333e4415a7SThomas Huth 343e4415a7SThomas Huth #include "s390-ccw.h" 353e4415a7SThomas Huth #include "virtio.h" 363e4415a7SThomas Huth 3729d12216SThomas Huth #define DEFAULT_BOOT_RETRIES 10 3829d12216SThomas Huth #define DEFAULT_TFTP_RETRIES 20 3929d12216SThomas 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 }; 4629d12216SThomas 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 7429d12216SThomas Huth /** 7529d12216SThomas Huth * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6). 7629d12216SThomas Huth * @param fn_ip contains the following configuration information: 7729d12216SThomas Huth * client MAC, client IP, TFTP-server MAC, TFTP-server IP, 7829d12216SThomas Huth * boot file name 7929d12216SThomas Huth * @param retries Number of DHCP attempts 8029d12216SThomas Huth * @return 0 : IP and configuration info obtained; 8129d12216SThomas Huth * non-0 : error condition occurred. 8229d12216SThomas Huth */ 8329d12216SThomas Huth static int dhcp(struct filename_ip *fn_ip, int retries) 8429d12216SThomas Huth { 8529d12216SThomas Huth int i = retries + 1; 8629d12216SThomas Huth int rc = -1; 8729d12216SThomas Huth 8829d12216SThomas Huth printf(" Requesting information via DHCP: "); 8929d12216SThomas Huth 9029d12216SThomas Huth dhcpv4_generate_transaction_id(); 9129d12216SThomas Huth dhcpv6_generate_transaction_id(); 9229d12216SThomas Huth 9329d12216SThomas Huth do { 9429d12216SThomas Huth printf("\b\b\b%03d", i - 1); 9529d12216SThomas Huth if (!--i) { 9629d12216SThomas Huth printf("\nGiving up after %d DHCP requests\n", retries); 9729d12216SThomas Huth return -1; 9829d12216SThomas Huth } 9929d12216SThomas Huth ip_version = 4; 10029d12216SThomas Huth rc = dhcpv4(NULL, fn_ip); 10129d12216SThomas Huth if (rc == -1) { 10229d12216SThomas Huth ip_version = 6; 10329d12216SThomas Huth set_ipv6_address(fn_ip->fd, 0); 10429d12216SThomas Huth rc = dhcpv6(NULL, fn_ip); 10529d12216SThomas Huth if (rc == 0) { 10629d12216SThomas Huth memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16); 10729d12216SThomas Huth break; 10829d12216SThomas Huth } 10929d12216SThomas Huth } 11029d12216SThomas Huth if (rc != -1) { /* either success or non-dhcp failure */ 11129d12216SThomas Huth break; 11229d12216SThomas Huth } 11329d12216SThomas Huth } while (1); 11429d12216SThomas Huth printf("\b\b\b\bdone\n"); 11529d12216SThomas Huth 11629d12216SThomas Huth return rc; 11729d12216SThomas Huth } 11829d12216SThomas Huth 11929d12216SThomas Huth /** 12029d12216SThomas Huth * Seed the random number generator with our mac and current timestamp 12129d12216SThomas Huth */ 12229d12216SThomas Huth static void seed_rng(uint8_t mac[]) 12329d12216SThomas Huth { 12429d12216SThomas Huth uint64_t seed; 12529d12216SThomas Huth 12629d12216SThomas Huth asm volatile(" stck %0 " : : "Q"(seed) : "memory"); 12729d12216SThomas Huth seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5]; 12829d12216SThomas Huth srand(seed); 12929d12216SThomas Huth } 13029d12216SThomas Huth 131*0c188229SThomas Huth static int tftp_load(filename_ip_t *fnip, void *buffer, int len) 13229d12216SThomas Huth { 13329d12216SThomas Huth tftp_err_t tftp_err; 13429d12216SThomas Huth int rc; 13529d12216SThomas Huth 136*0c188229SThomas Huth rc = tftp(fnip, buffer, len, DEFAULT_TFTP_RETRIES, &tftp_err, 1, 1428, 137*0c188229SThomas Huth ip_version); 13829d12216SThomas Huth 13929d12216SThomas Huth if (rc > 0) { 14029d12216SThomas Huth printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, 14129d12216SThomas Huth rc / 1024); 14229d12216SThomas Huth } else if (rc == -1) { 14329d12216SThomas Huth puts("unknown TFTP error"); 14429d12216SThomas Huth } else if (rc == -2) { 14529d12216SThomas Huth printf("TFTP buffer of %d bytes is too small for %s\n", 14629d12216SThomas Huth len, fnip->filename); 14729d12216SThomas Huth } else if (rc == -3) { 14829d12216SThomas Huth printf("file not found: %s\n", fnip->filename); 14929d12216SThomas Huth } else if (rc == -4) { 15029d12216SThomas Huth puts("TFTP access violation"); 15129d12216SThomas Huth } else if (rc == -5) { 15229d12216SThomas Huth puts("illegal TFTP operation"); 15329d12216SThomas Huth } else if (rc == -6) { 15429d12216SThomas Huth puts("unknown TFTP transfer ID"); 15529d12216SThomas Huth } else if (rc == -7) { 15629d12216SThomas Huth puts("no such TFTP user"); 15729d12216SThomas Huth } else if (rc == -8) { 15829d12216SThomas Huth puts("TFTP blocksize negotiation failed"); 15929d12216SThomas Huth } else if (rc == -9) { 16029d12216SThomas Huth puts("file exceeds maximum TFTP transfer size"); 16129d12216SThomas Huth } else if (rc <= -10 && rc >= -15) { 16229d12216SThomas Huth const char *icmp_err_str; 16329d12216SThomas Huth switch (rc) { 16429d12216SThomas Huth case -ICMP_NET_UNREACHABLE - 10: 16529d12216SThomas Huth icmp_err_str = "net unreachable"; 16629d12216SThomas Huth break; 16729d12216SThomas Huth case -ICMP_HOST_UNREACHABLE - 10: 16829d12216SThomas Huth icmp_err_str = "host unreachable"; 16929d12216SThomas Huth break; 17029d12216SThomas Huth case -ICMP_PROTOCOL_UNREACHABLE - 10: 17129d12216SThomas Huth icmp_err_str = "protocol unreachable"; 17229d12216SThomas Huth break; 17329d12216SThomas Huth case -ICMP_PORT_UNREACHABLE - 10: 17429d12216SThomas Huth icmp_err_str = "port unreachable"; 17529d12216SThomas Huth break; 17629d12216SThomas Huth case -ICMP_FRAGMENTATION_NEEDED - 10: 17729d12216SThomas Huth icmp_err_str = "fragmentation needed and DF set"; 17829d12216SThomas Huth break; 17929d12216SThomas Huth case -ICMP_SOURCE_ROUTE_FAILED - 10: 18029d12216SThomas Huth icmp_err_str = "source route failed"; 18129d12216SThomas Huth break; 18229d12216SThomas Huth default: 18329d12216SThomas Huth icmp_err_str = " UNKNOWN"; 18429d12216SThomas Huth break; 18529d12216SThomas Huth } 18629d12216SThomas Huth printf("ICMP ERROR \"%s\"\n", icmp_err_str); 18729d12216SThomas Huth } else if (rc == -40) { 18829d12216SThomas Huth printf("TFTP error occurred after %d bad packets received", 18929d12216SThomas Huth tftp_err.bad_tftp_packets); 19029d12216SThomas Huth } else if (rc == -41) { 19129d12216SThomas Huth printf("TFTP error occurred after missing %d responses", 19229d12216SThomas Huth tftp_err.no_packets); 19329d12216SThomas Huth } else if (rc == -42) { 19429d12216SThomas Huth printf("TFTP error missing block %d, expected block was %d", 19529d12216SThomas Huth tftp_err.blocks_missed, 19629d12216SThomas Huth tftp_err.blocks_received); 19729d12216SThomas Huth } 19829d12216SThomas Huth 19929d12216SThomas Huth return rc; 20029d12216SThomas Huth } 20129d12216SThomas Huth 202*0c188229SThomas Huth static int net_init(filename_ip_t *fn_ip) 20329d12216SThomas Huth { 20429d12216SThomas Huth uint8_t mac[6]; 20529d12216SThomas Huth int rc; 20629d12216SThomas Huth 207*0c188229SThomas Huth memset(fn_ip, 0, sizeof(filename_ip_t)); 20829d12216SThomas Huth 20929d12216SThomas Huth rc = virtio_net_init(mac); 21029d12216SThomas Huth if (rc < 0) { 21129d12216SThomas Huth puts("Could not initialize network device"); 21229d12216SThomas Huth return -101; 21329d12216SThomas Huth } 214*0c188229SThomas Huth fn_ip->fd = rc; 21529d12216SThomas Huth 21629d12216SThomas Huth printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n", 21729d12216SThomas Huth mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 21829d12216SThomas Huth 21929d12216SThomas Huth set_mac_address(mac); /* init ethernet layer */ 22029d12216SThomas Huth seed_rng(mac); 22129d12216SThomas Huth 222*0c188229SThomas Huth rc = dhcp(fn_ip, DEFAULT_BOOT_RETRIES); 22329d12216SThomas Huth if (rc >= 0) { 22429d12216SThomas Huth if (ip_version == 4) { 225*0c188229SThomas Huth set_ipv4_address(fn_ip->own_ip); 22629d12216SThomas Huth } 22729d12216SThomas Huth } else { 22829d12216SThomas Huth puts("Could not get IP address"); 22929d12216SThomas Huth return -101; 23029d12216SThomas Huth } 23129d12216SThomas Huth 23229d12216SThomas Huth if (ip_version == 4) { 23329d12216SThomas Huth printf(" Using IPv4 address: %d.%d.%d.%d\n", 234*0c188229SThomas Huth (fn_ip->own_ip >> 24) & 0xFF, (fn_ip->own_ip >> 16) & 0xFF, 235*0c188229SThomas Huth (fn_ip->own_ip >> 8) & 0xFF, fn_ip->own_ip & 0xFF); 23629d12216SThomas Huth } else if (ip_version == 6) { 23729d12216SThomas Huth char ip6_str[40]; 238*0c188229SThomas Huth ipv6_to_str(fn_ip->own_ip6.addr, ip6_str); 23929d12216SThomas Huth printf(" Using IPv6 address: %s\n", ip6_str); 24029d12216SThomas Huth } 24129d12216SThomas Huth 24229d12216SThomas Huth if (rc == -2) { 24329d12216SThomas Huth printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n", 244*0c188229SThomas Huth (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF, 245*0c188229SThomas Huth (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF); 24629d12216SThomas Huth return -102; 24729d12216SThomas Huth } 24829d12216SThomas Huth if (rc == -4 || rc == -3) { 24929d12216SThomas Huth puts("Can't obtain TFTP server IP address"); 25029d12216SThomas Huth return -107; 25129d12216SThomas Huth } 25229d12216SThomas Huth 253*0c188229SThomas Huth printf(" Using TFTP server: "); 25429d12216SThomas Huth if (ip_version == 4) { 255*0c188229SThomas Huth printf("%d.%d.%d.%d\n", 256*0c188229SThomas Huth (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF, 257*0c188229SThomas Huth (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF); 25829d12216SThomas Huth } else if (ip_version == 6) { 25929d12216SThomas Huth char ip6_str[40]; 260*0c188229SThomas Huth ipv6_to_str(fn_ip->server_ip6.addr, ip6_str); 26129d12216SThomas Huth printf("%s\n", ip6_str); 26229d12216SThomas Huth } 26329d12216SThomas Huth 264*0c188229SThomas Huth if (strlen((char *)fn_ip->filename) > 0) { 265*0c188229SThomas Huth printf(" Bootfile name: '%s'\n", fn_ip->filename); 26629d12216SThomas Huth } 26729d12216SThomas Huth 26829d12216SThomas Huth return rc; 26929d12216SThomas Huth } 27029d12216SThomas Huth 271*0c188229SThomas Huth static void net_release(filename_ip_t *fn_ip) 272*0c188229SThomas Huth { 273*0c188229SThomas Huth if (ip_version == 4) { 274*0c188229SThomas Huth dhcp_send_release(fn_ip->fd); 275*0c188229SThomas Huth } 276*0c188229SThomas Huth } 277*0c188229SThomas Huth 2783e4415a7SThomas Huth void panic(const char *string) 2793e4415a7SThomas Huth { 2803e4415a7SThomas Huth sclp_print(string); 2813e4415a7SThomas Huth for (;;) { 2823e4415a7SThomas Huth disabled_wait(); 2833e4415a7SThomas Huth } 2843e4415a7SThomas Huth } 2853e4415a7SThomas Huth 2863e4415a7SThomas Huth static bool find_net_dev(Schib *schib, int dev_no) 2873e4415a7SThomas Huth { 2883e4415a7SThomas Huth int i, r; 2893e4415a7SThomas Huth 2903e4415a7SThomas Huth for (i = 0; i < 0x10000; i++) { 2913e4415a7SThomas Huth net_schid.sch_no = i; 2923e4415a7SThomas Huth r = stsch_err(net_schid, schib); 2933e4415a7SThomas Huth if (r == 3 || r == -EIO) { 2943e4415a7SThomas Huth break; 2953e4415a7SThomas Huth } 2963e4415a7SThomas Huth if (!schib->pmcw.dnv) { 2973e4415a7SThomas Huth continue; 2983e4415a7SThomas Huth } 2993e4415a7SThomas Huth if (!virtio_is_supported(net_schid)) { 3003e4415a7SThomas Huth continue; 3013e4415a7SThomas Huth } 3023e4415a7SThomas Huth if (virtio_get_device_type() != VIRTIO_ID_NET) { 3033e4415a7SThomas Huth continue; 3043e4415a7SThomas Huth } 3053e4415a7SThomas Huth if (dev_no < 0 || schib->pmcw.dev == dev_no) { 3063e4415a7SThomas Huth return true; 3073e4415a7SThomas Huth } 3083e4415a7SThomas Huth } 3093e4415a7SThomas Huth 3103e4415a7SThomas Huth return false; 3113e4415a7SThomas Huth } 3123e4415a7SThomas Huth 3133e4415a7SThomas Huth static void virtio_setup(void) 3143e4415a7SThomas Huth { 3153e4415a7SThomas Huth Schib schib; 3163e4415a7SThomas Huth int ssid; 3173e4415a7SThomas Huth bool found = false; 3183e4415a7SThomas Huth uint16_t dev_no; 3193e4415a7SThomas Huth 3203e4415a7SThomas Huth /* 3213e4415a7SThomas Huth * We unconditionally enable mss support. In every sane configuration, 3223e4415a7SThomas Huth * this will succeed; and even if it doesn't, stsch_err() can deal 3233e4415a7SThomas Huth * with the consequences. 3243e4415a7SThomas Huth */ 3253e4415a7SThomas Huth enable_mss_facility(); 3263e4415a7SThomas Huth 3273e4415a7SThomas Huth if (store_iplb(&iplb)) { 3283e4415a7SThomas Huth IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected"); 3293e4415a7SThomas Huth dev_no = iplb.ccw.devno; 3303e4415a7SThomas Huth debug_print_int("device no. ", dev_no); 3313e4415a7SThomas Huth net_schid.ssid = iplb.ccw.ssid & 0x3; 3323e4415a7SThomas Huth debug_print_int("ssid ", net_schid.ssid); 3333e4415a7SThomas Huth found = find_net_dev(&schib, dev_no); 3343e4415a7SThomas Huth } else { 3353e4415a7SThomas Huth for (ssid = 0; ssid < 0x3; ssid++) { 3363e4415a7SThomas Huth net_schid.ssid = ssid; 3373e4415a7SThomas Huth found = find_net_dev(&schib, -1); 3383e4415a7SThomas Huth if (found) { 3393e4415a7SThomas Huth break; 3403e4415a7SThomas Huth } 3413e4415a7SThomas Huth } 3423e4415a7SThomas Huth } 3433e4415a7SThomas Huth 3443e4415a7SThomas Huth IPL_assert(found, "No virtio net device found"); 3453e4415a7SThomas Huth } 3463e4415a7SThomas Huth 3473e4415a7SThomas Huth void main(void) 3483e4415a7SThomas Huth { 349*0c188229SThomas Huth filename_ip_t fn_ip; 35029d12216SThomas Huth int rc; 35129d12216SThomas Huth 3523e4415a7SThomas Huth sclp_setup(); 3533e4415a7SThomas Huth sclp_print("Network boot starting...\n"); 3543e4415a7SThomas Huth 3553e4415a7SThomas Huth virtio_setup(); 3563e4415a7SThomas Huth 357*0c188229SThomas Huth rc = net_init(&fn_ip); 358*0c188229SThomas Huth if (rc) { 359*0c188229SThomas Huth panic("Network initialization failed. Halting.\n"); 360*0c188229SThomas Huth } 361*0c188229SThomas Huth 362*0c188229SThomas Huth rc = tftp_load(&fn_ip, NULL, (long)_start); 363*0c188229SThomas Huth 364*0c188229SThomas Huth net_release(&fn_ip); 365*0c188229SThomas Huth 36629d12216SThomas Huth if (rc > 0) { 36729d12216SThomas Huth sclp_print("Network loading done, starting kernel...\n"); 36829d12216SThomas Huth asm volatile (" lpsw 0(%0) " : : "r"(0) : "memory"); 36929d12216SThomas Huth } 37029d12216SThomas Huth 3713e4415a7SThomas Huth panic("Failed to load OS from network\n"); 3723e4415a7SThomas Huth } 373