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 34 #include "s390-ccw.h" 35 #include "virtio.h" 36 37 #define DEFAULT_BOOT_RETRIES 10 38 #define DEFAULT_TFTP_RETRIES 20 39 40 extern char _start[]; 41 42 #define KERNEL_ADDR ((void *)0L) 43 #define KERNEL_MAX_SIZE ((long)_start) 44 45 char stack[PAGE_SIZE * 8] __attribute__((aligned(PAGE_SIZE))); 46 IplParameterBlock iplb __attribute__((aligned(PAGE_SIZE))); 47 static char cfgbuf[2048]; 48 49 static SubChannelId net_schid = { .one = 1 }; 50 static int ip_version = 4; 51 static uint64_t dest_timer; 52 53 static uint64_t get_timer_ms(void) 54 { 55 uint64_t clk; 56 57 asm volatile(" stck %0 " : : "Q"(clk) : "memory"); 58 59 /* Bit 51 is incremented each microsecond */ 60 return (clk >> (63 - 51)) / 1000; 61 } 62 63 void set_timer(int val) 64 { 65 dest_timer = get_timer_ms() + val; 66 } 67 68 int get_timer(void) 69 { 70 return dest_timer - get_timer_ms(); 71 } 72 73 int get_sec_ticks(void) 74 { 75 return 1000; /* number of ticks in 1 second */ 76 } 77 78 /** 79 * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6). 80 * @param fn_ip contains the following configuration information: 81 * client MAC, client IP, TFTP-server MAC, TFTP-server IP, 82 * boot file name 83 * @param retries Number of DHCP attempts 84 * @return 0 : IP and configuration info obtained; 85 * non-0 : error condition occurred. 86 */ 87 static int dhcp(struct filename_ip *fn_ip, int retries) 88 { 89 int i = retries + 1; 90 int rc = -1; 91 92 printf(" Requesting information via DHCP: "); 93 94 dhcpv4_generate_transaction_id(); 95 dhcpv6_generate_transaction_id(); 96 97 do { 98 printf("\b\b\b%03d", i - 1); 99 if (!--i) { 100 printf("\nGiving up after %d DHCP requests\n", retries); 101 return -1; 102 } 103 ip_version = 4; 104 rc = dhcpv4(NULL, fn_ip); 105 if (rc == -1) { 106 ip_version = 6; 107 set_ipv6_address(fn_ip->fd, 0); 108 rc = dhcpv6(NULL, fn_ip); 109 if (rc == 0) { 110 memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16); 111 break; 112 } 113 } 114 if (rc != -1) { /* either success or non-dhcp failure */ 115 break; 116 } 117 } while (1); 118 printf("\b\b\b\bdone\n"); 119 120 return rc; 121 } 122 123 /** 124 * Seed the random number generator with our mac and current timestamp 125 */ 126 static void seed_rng(uint8_t mac[]) 127 { 128 uint64_t seed; 129 130 asm volatile(" stck %0 " : : "Q"(seed) : "memory"); 131 seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5]; 132 srand(seed); 133 } 134 135 static int tftp_load(filename_ip_t *fnip, void *buffer, int len) 136 { 137 tftp_err_t tftp_err; 138 int rc; 139 140 rc = tftp(fnip, buffer, len, DEFAULT_TFTP_RETRIES, &tftp_err, 1, 1428, 141 ip_version); 142 143 if (rc < 0) { 144 /* Make sure that error messages are put into a new line */ 145 printf("\n "); 146 } 147 148 if (rc > 1024) { 149 printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, rc / 1024); 150 } else if (rc > 0) { 151 printf(" TFTP: Received %s (%d Bytes)\n", fnip->filename, rc); 152 } else if (rc == -1) { 153 puts("unknown TFTP error"); 154 } else if (rc == -2) { 155 printf("TFTP buffer of %d bytes is too small for %s\n", 156 len, fnip->filename); 157 } else if (rc == -3) { 158 printf("file not found: %s\n", fnip->filename); 159 } else if (rc == -4) { 160 puts("TFTP access violation"); 161 } else if (rc == -5) { 162 puts("illegal TFTP operation"); 163 } else if (rc == -6) { 164 puts("unknown TFTP transfer ID"); 165 } else if (rc == -7) { 166 puts("no such TFTP user"); 167 } else if (rc == -8) { 168 puts("TFTP blocksize negotiation failed"); 169 } else if (rc == -9) { 170 puts("file exceeds maximum TFTP transfer size"); 171 } else if (rc <= -10 && rc >= -15) { 172 const char *icmp_err_str; 173 switch (rc) { 174 case -ICMP_NET_UNREACHABLE - 10: 175 icmp_err_str = "net unreachable"; 176 break; 177 case -ICMP_HOST_UNREACHABLE - 10: 178 icmp_err_str = "host unreachable"; 179 break; 180 case -ICMP_PROTOCOL_UNREACHABLE - 10: 181 icmp_err_str = "protocol unreachable"; 182 break; 183 case -ICMP_PORT_UNREACHABLE - 10: 184 icmp_err_str = "port unreachable"; 185 break; 186 case -ICMP_FRAGMENTATION_NEEDED - 10: 187 icmp_err_str = "fragmentation needed and DF set"; 188 break; 189 case -ICMP_SOURCE_ROUTE_FAILED - 10: 190 icmp_err_str = "source route failed"; 191 break; 192 default: 193 icmp_err_str = " UNKNOWN"; 194 break; 195 } 196 printf("ICMP ERROR \"%s\"\n", icmp_err_str); 197 } else if (rc == -40) { 198 printf("TFTP error occurred after %d bad packets received", 199 tftp_err.bad_tftp_packets); 200 } else if (rc == -41) { 201 printf("TFTP error occurred after missing %d responses", 202 tftp_err.no_packets); 203 } else if (rc == -42) { 204 printf("TFTP error missing block %d, expected block was %d", 205 tftp_err.blocks_missed, 206 tftp_err.blocks_received); 207 } 208 209 return rc; 210 } 211 212 static int net_init(filename_ip_t *fn_ip) 213 { 214 uint8_t mac[6]; 215 int rc; 216 217 memset(fn_ip, 0, sizeof(filename_ip_t)); 218 219 rc = virtio_net_init(mac); 220 if (rc < 0) { 221 puts("Could not initialize network device"); 222 return -101; 223 } 224 fn_ip->fd = rc; 225 226 printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n", 227 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 228 229 set_mac_address(mac); /* init ethernet layer */ 230 seed_rng(mac); 231 232 rc = dhcp(fn_ip, DEFAULT_BOOT_RETRIES); 233 if (rc >= 0) { 234 if (ip_version == 4) { 235 set_ipv4_address(fn_ip->own_ip); 236 } 237 } else { 238 puts("Could not get IP address"); 239 return -101; 240 } 241 242 if (ip_version == 4) { 243 printf(" Using IPv4 address: %d.%d.%d.%d\n", 244 (fn_ip->own_ip >> 24) & 0xFF, (fn_ip->own_ip >> 16) & 0xFF, 245 (fn_ip->own_ip >> 8) & 0xFF, fn_ip->own_ip & 0xFF); 246 } else if (ip_version == 6) { 247 char ip6_str[40]; 248 ipv6_to_str(fn_ip->own_ip6.addr, ip6_str); 249 printf(" Using IPv6 address: %s\n", ip6_str); 250 } 251 252 if (rc == -2) { 253 printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n", 254 (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF, 255 (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF); 256 return -102; 257 } 258 if (rc == -4 || rc == -3) { 259 puts("Can't obtain TFTP server IP address"); 260 return -107; 261 } 262 263 printf(" Using TFTP server: "); 264 if (ip_version == 4) { 265 printf("%d.%d.%d.%d\n", 266 (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF, 267 (fn_ip->server_ip >> 8) & 0xFF, fn_ip->server_ip & 0xFF); 268 } else if (ip_version == 6) { 269 char ip6_str[40]; 270 ipv6_to_str(fn_ip->server_ip6.addr, ip6_str); 271 printf("%s\n", ip6_str); 272 } 273 274 if (strlen((char *)fn_ip->filename) > 0) { 275 printf(" Bootfile name: '%s'\n", fn_ip->filename); 276 } 277 278 return rc; 279 } 280 281 static void net_release(filename_ip_t *fn_ip) 282 { 283 if (ip_version == 4) { 284 dhcp_send_release(fn_ip->fd); 285 } 286 } 287 288 /** 289 * Load via information from a .INS file (which can be found on CD-ROMs 290 * for example) 291 */ 292 static int handle_ins_cfg(filename_ip_t *fn_ip, char *cfg, int cfgsize) 293 { 294 char *ptr; 295 int rc = -1, llen; 296 void *destaddr; 297 char *insbuf = cfg; 298 299 ptr = strchr(insbuf, '\n'); 300 if (!ptr) { 301 puts("Does not seem to be a valid .INS file"); 302 return -1; 303 } 304 305 *ptr = 0; 306 printf("\nParsing .INS file:\n %s\n", &insbuf[2]); 307 308 insbuf = ptr + 1; 309 while (*insbuf && insbuf < cfg + cfgsize) { 310 ptr = strchr(insbuf, '\n'); 311 if (ptr) { 312 *ptr = 0; 313 } 314 llen = strlen(insbuf); 315 if (!llen) { 316 insbuf = ptr + 1; 317 continue; 318 } 319 ptr = strchr(insbuf, ' '); 320 if (!ptr) { 321 puts("Missing space separator in .INS file"); 322 return -1; 323 } 324 *ptr = 0; 325 strncpy((char *)fn_ip->filename, insbuf, sizeof(fn_ip->filename)); 326 destaddr = (char *)atol(ptr + 1); 327 rc = tftp_load(fn_ip, destaddr, (long)_start - (long)destaddr); 328 if (rc <= 0) { 329 break; 330 } 331 insbuf += llen + 1; 332 } 333 334 return rc; 335 } 336 337 static int net_try_direct_tftp_load(filename_ip_t *fn_ip) 338 { 339 int rc; 340 void *loadaddr = (void *)0x2000; /* Load right after the low-core */ 341 342 rc = tftp_load(fn_ip, loadaddr, KERNEL_MAX_SIZE - (long)loadaddr); 343 if (rc < 0) { 344 return rc; 345 } else if (rc < 8) { 346 printf("'%s' is too small (%i bytes only).\n", fn_ip->filename, rc); 347 return -1; 348 } 349 350 /* Check whether it is a configuration file instead of a kernel */ 351 if (rc < sizeof(cfgbuf) - 1) { 352 memcpy(cfgbuf, loadaddr, rc); 353 cfgbuf[rc] = 0; /* Make sure that it is NUL-terminated */ 354 if (!strncmp("* ", cfgbuf, 2)) { 355 return handle_ins_cfg(fn_ip, cfgbuf, rc); 356 } 357 } 358 359 /* Move kernel to right location */ 360 memmove(KERNEL_ADDR, loadaddr, rc); 361 362 return rc; 363 } 364 365 void panic(const char *string) 366 { 367 sclp_print(string); 368 for (;;) { 369 disabled_wait(); 370 } 371 } 372 373 void write_subsystem_identification(void) 374 { 375 SubChannelId *schid = (SubChannelId *) 184; 376 uint32_t *zeroes = (uint32_t *) 188; 377 378 *schid = net_schid; 379 *zeroes = 0; 380 } 381 382 static bool find_net_dev(Schib *schib, int dev_no) 383 { 384 int i, r; 385 386 for (i = 0; i < 0x10000; i++) { 387 net_schid.sch_no = i; 388 r = stsch_err(net_schid, schib); 389 if (r == 3 || r == -EIO) { 390 break; 391 } 392 if (!schib->pmcw.dnv) { 393 continue; 394 } 395 if (!virtio_is_supported(net_schid)) { 396 continue; 397 } 398 if (virtio_get_device_type() != VIRTIO_ID_NET) { 399 continue; 400 } 401 if (dev_no < 0 || schib->pmcw.dev == dev_no) { 402 return true; 403 } 404 } 405 406 return false; 407 } 408 409 static void virtio_setup(void) 410 { 411 Schib schib; 412 int ssid; 413 bool found = false; 414 uint16_t dev_no; 415 416 /* 417 * We unconditionally enable mss support. In every sane configuration, 418 * this will succeed; and even if it doesn't, stsch_err() can deal 419 * with the consequences. 420 */ 421 enable_mss_facility(); 422 423 if (store_iplb(&iplb)) { 424 IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected"); 425 dev_no = iplb.ccw.devno; 426 debug_print_int("device no. ", dev_no); 427 net_schid.ssid = iplb.ccw.ssid & 0x3; 428 debug_print_int("ssid ", net_schid.ssid); 429 found = find_net_dev(&schib, dev_no); 430 } else { 431 for (ssid = 0; ssid < 0x3; ssid++) { 432 net_schid.ssid = ssid; 433 found = find_net_dev(&schib, -1); 434 if (found) { 435 break; 436 } 437 } 438 } 439 440 IPL_assert(found, "No virtio net device found"); 441 } 442 443 void main(void) 444 { 445 filename_ip_t fn_ip; 446 int rc, fnlen; 447 448 sclp_setup(); 449 sclp_print("Network boot starting...\n"); 450 451 virtio_setup(); 452 453 rc = net_init(&fn_ip); 454 if (rc) { 455 panic("Network initialization failed. Halting.\n"); 456 } 457 458 fnlen = strlen((char *)fn_ip.filename); 459 if (fnlen > 0 && fn_ip.filename[fnlen - 1] != '/') { 460 rc = net_try_direct_tftp_load(&fn_ip); 461 } 462 463 net_release(&fn_ip); 464 465 if (rc > 0) { 466 sclp_print("Network loading done, starting kernel...\n"); 467 jump_to_low_kernel(); 468 } 469 470 panic("Failed to load OS from network\n"); 471 } 472