2 * S390 virtio-ccw network boot loading program
4 * Copyright 2017 Thomas Huth, Red Hat Inc.
6 * Based on the S390 virtio-ccw loading program (main.c)
7 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
9 * And based on the network loading code from SLOF (netload.c)
10 * Copyright (c) 2004, 2008 IBM Corporation
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.
37 #define DEFAULT_BOOT_RETRIES 10
38 #define DEFAULT_TFTP_RETRIES 20
42 #define KERNEL_ADDR ((void *)0L)
43 #define KERNEL_MAX_SIZE ((long)_start)
45 char stack
[PAGE_SIZE
* 8] __attribute__((aligned(PAGE_SIZE
)));
46 IplParameterBlock iplb
__attribute__((aligned(PAGE_SIZE
)));
47 static char cfgbuf
[2048];
49 static SubChannelId net_schid
= { .one
= 1 };
50 static int ip_version
= 4;
51 static uint64_t dest_timer
;
53 static uint64_t get_timer_ms(void)
57 asm volatile(" stck %0 " : : "Q"(clk
) : "memory");
59 /* Bit 51 is incremented each microsecond */
60 return (clk
>> (63 - 51)) / 1000;
63 void set_timer(int val
)
65 dest_timer
= get_timer_ms() + val
;
70 return dest_timer
- get_timer_ms();
73 int get_sec_ticks(void)
75 return 1000; /* number of ticks in 1 second */
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,
83 * @param retries Number of DHCP attempts
84 * @return 0 : IP and configuration info obtained;
85 * non-0 : error condition occurred.
87 static int dhcp(struct filename_ip
*fn_ip
, int retries
)
92 printf(" Requesting information via DHCP: ");
94 dhcpv4_generate_transaction_id();
95 dhcpv6_generate_transaction_id();
98 printf("\b\b\b%03d", i
- 1);
100 printf("\nGiving up after %d DHCP requests\n", retries
);
104 rc
= dhcpv4(NULL
, fn_ip
);
107 set_ipv6_address(fn_ip
->fd
, 0);
108 rc
= dhcpv6(NULL
, fn_ip
);
110 memcpy(&fn_ip
->own_ip6
, get_ipv6_address(), 16);
114 if (rc
!= -1) { /* either success or non-dhcp failure */
118 printf("\b\b\b\bdone\n");
124 * Seed the random number generator with our mac and current timestamp
126 static void seed_rng(uint8_t mac
[])
130 asm volatile(" stck %0 " : : "Q"(seed
) : "memory");
131 seed
^= (mac
[2] << 24) | (mac
[3] << 16) | (mac
[4] << 8) | mac
[5];
135 static int tftp_load(filename_ip_t
*fnip
, void *buffer
, int len
)
140 rc
= tftp(fnip
, buffer
, len
, DEFAULT_TFTP_RETRIES
, &tftp_err
, 1, 1428,
144 /* Make sure that error messages are put into a new line */
149 printf(" TFTP: Received %s (%d KBytes)\n", fnip
->filename
, rc
/ 1024);
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
;
174 case -ICMP_NET_UNREACHABLE
- 10:
175 icmp_err_str
= "net unreachable";
177 case -ICMP_HOST_UNREACHABLE
- 10:
178 icmp_err_str
= "host unreachable";
180 case -ICMP_PROTOCOL_UNREACHABLE
- 10:
181 icmp_err_str
= "protocol unreachable";
183 case -ICMP_PORT_UNREACHABLE
- 10:
184 icmp_err_str
= "port unreachable";
186 case -ICMP_FRAGMENTATION_NEEDED
- 10:
187 icmp_err_str
= "fragmentation needed and DF set";
189 case -ICMP_SOURCE_ROUTE_FAILED
- 10:
190 icmp_err_str
= "source route failed";
193 icmp_err_str
= " UNKNOWN";
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
);
212 static int net_init(filename_ip_t
*fn_ip
)
217 memset(fn_ip
, 0, sizeof(filename_ip_t
));
219 rc
= virtio_net_init(mac
);
221 puts("Could not initialize network device");
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]);
229 set_mac_address(mac
); /* init ethernet layer */
232 rc
= dhcp(fn_ip
, DEFAULT_BOOT_RETRIES
);
234 if (ip_version
== 4) {
235 set_ipv4_address(fn_ip
->own_ip
);
238 puts("Could not get IP address");
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) {
248 ipv6_to_str(fn_ip
->own_ip6
.addr
, ip6_str
);
249 printf(" Using IPv6 address: %s\n", ip6_str
);
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);
258 if (rc
== -4 || rc
== -3) {
259 puts("Can't obtain TFTP server IP address");
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) {
270 ipv6_to_str(fn_ip
->server_ip6
.addr
, ip6_str
);
271 printf("%s\n", ip6_str
);
274 if (strlen((char *)fn_ip
->filename
) > 0) {
275 printf(" Bootfile name: '%s'\n", fn_ip
->filename
);
281 static void net_release(filename_ip_t
*fn_ip
)
283 if (ip_version
== 4) {
284 dhcp_send_release(fn_ip
->fd
);
289 * Load via information from a .INS file (which can be found on CD-ROMs
292 static int handle_ins_cfg(filename_ip_t
*fn_ip
, char *cfg
, int cfgsize
)
299 ptr
= strchr(insbuf
, '\n');
301 puts("Does not seem to be a valid .INS file");
306 printf("\nParsing .INS file:\n %s\n", &insbuf
[2]);
309 while (*insbuf
&& insbuf
< cfg
+ cfgsize
) {
310 ptr
= strchr(insbuf
, '\n');
314 llen
= strlen(insbuf
);
319 ptr
= strchr(insbuf
, ' ');
321 puts("Missing space separator in .INS file");
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
);
337 static int net_try_direct_tftp_load(filename_ip_t
*fn_ip
)
340 void *loadaddr
= (void *)0x2000; /* Load right after the low-core */
342 rc
= tftp_load(fn_ip
, loadaddr
, KERNEL_MAX_SIZE
- (long)loadaddr
);
346 printf("'%s' is too small (%i bytes only).\n", fn_ip
->filename
, rc
);
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
);
359 /* Move kernel to right location */
360 memmove(KERNEL_ADDR
, loadaddr
, rc
);
365 void panic(const char *string
)
373 void write_subsystem_identification(void)
375 SubChannelId
*schid
= (SubChannelId
*) 184;
376 uint32_t *zeroes
= (uint32_t *) 188;
382 static bool find_net_dev(Schib
*schib
, int dev_no
)
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
) {
392 if (!schib
->pmcw
.dnv
) {
395 if (!virtio_is_supported(net_schid
)) {
398 if (virtio_get_device_type() != VIRTIO_ID_NET
) {
401 if (dev_no
< 0 || schib
->pmcw
.dev
== dev_no
) {
409 static void virtio_setup(void)
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.
421 enable_mss_facility();
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
);
431 for (ssid
= 0; ssid
< 0x3; ssid
++) {
432 net_schid
.ssid
= ssid
;
433 found
= find_net_dev(&schib
, -1);
440 IPL_assert(found
, "No virtio net device found");
449 sclp_print("Network boot starting...\n");
453 rc
= net_init(&fn_ip
);
455 panic("Network initialization failed. Halting.\n");
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
);
466 sclp_print("Network loading done, starting kernel...\n");
467 jump_to_low_kernel();
470 panic("Failed to load OS from network\n");