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