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 char stack
[PAGE_SIZE
* 8] __attribute__((aligned(PAGE_SIZE
)));
43 IplParameterBlock iplb
__attribute__((aligned(PAGE_SIZE
)));
45 static SubChannelId net_schid
= { .one
= 1 };
46 static int ip_version
= 4;
47 static uint64_t dest_timer
;
49 static uint64_t get_timer_ms(void)
53 asm volatile(" stck %0 " : : "Q"(clk
) : "memory");
55 /* Bit 51 is incremented each microsecond */
56 return (clk
>> (63 - 51)) / 1000;
59 void set_timer(int val
)
61 dest_timer
= get_timer_ms() + val
;
66 return dest_timer
- get_timer_ms();
69 int get_sec_ticks(void)
71 return 1000; /* number of ticks in 1 second */
75 * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6).
76 * @param fn_ip contains the following configuration information:
77 * client MAC, client IP, TFTP-server MAC, TFTP-server IP,
79 * @param retries Number of DHCP attempts
80 * @return 0 : IP and configuration info obtained;
81 * non-0 : error condition occurred.
83 static int dhcp(struct filename_ip
*fn_ip
, int retries
)
88 printf(" Requesting information via DHCP: ");
90 dhcpv4_generate_transaction_id();
91 dhcpv6_generate_transaction_id();
94 printf("\b\b\b%03d", i
- 1);
96 printf("\nGiving up after %d DHCP requests\n", retries
);
100 rc
= dhcpv4(NULL
, fn_ip
);
103 set_ipv6_address(fn_ip
->fd
, 0);
104 rc
= dhcpv6(NULL
, fn_ip
);
106 memcpy(&fn_ip
->own_ip6
, get_ipv6_address(), 16);
110 if (rc
!= -1) { /* either success or non-dhcp failure */
114 printf("\b\b\b\bdone\n");
120 * Seed the random number generator with our mac and current timestamp
122 static void seed_rng(uint8_t mac
[])
126 asm volatile(" stck %0 " : : "Q"(seed
) : "memory");
127 seed
^= (mac
[2] << 24) | (mac
[3] << 16) | (mac
[4] << 8) | mac
[5];
131 static int tftp_load(filename_ip_t
*fnip
, void *buffer
, int len
,
132 unsigned int retries
, int ip_vers
)
137 rc
= tftp(fnip
, buffer
, len
, retries
, &tftp_err
, 1, 1428, ip_vers
);
140 printf(" TFTP: Received %s (%d KBytes)\n", fnip
->filename
,
142 } else if (rc
== -1) {
143 puts("unknown TFTP error");
144 } else if (rc
== -2) {
145 printf("TFTP buffer of %d bytes is too small for %s\n",
146 len
, fnip
->filename
);
147 } else if (rc
== -3) {
148 printf("file not found: %s\n", fnip
->filename
);
149 } else if (rc
== -4) {
150 puts("TFTP access violation");
151 } else if (rc
== -5) {
152 puts("illegal TFTP operation");
153 } else if (rc
== -6) {
154 puts("unknown TFTP transfer ID");
155 } else if (rc
== -7) {
156 puts("no such TFTP user");
157 } else if (rc
== -8) {
158 puts("TFTP blocksize negotiation failed");
159 } else if (rc
== -9) {
160 puts("file exceeds maximum TFTP transfer size");
161 } else if (rc
<= -10 && rc
>= -15) {
162 const char *icmp_err_str
;
164 case -ICMP_NET_UNREACHABLE
- 10:
165 icmp_err_str
= "net unreachable";
167 case -ICMP_HOST_UNREACHABLE
- 10:
168 icmp_err_str
= "host unreachable";
170 case -ICMP_PROTOCOL_UNREACHABLE
- 10:
171 icmp_err_str
= "protocol unreachable";
173 case -ICMP_PORT_UNREACHABLE
- 10:
174 icmp_err_str
= "port unreachable";
176 case -ICMP_FRAGMENTATION_NEEDED
- 10:
177 icmp_err_str
= "fragmentation needed and DF set";
179 case -ICMP_SOURCE_ROUTE_FAILED
- 10:
180 icmp_err_str
= "source route failed";
183 icmp_err_str
= " UNKNOWN";
186 printf("ICMP ERROR \"%s\"\n", icmp_err_str
);
187 } else if (rc
== -40) {
188 printf("TFTP error occurred after %d bad packets received",
189 tftp_err
.bad_tftp_packets
);
190 } else if (rc
== -41) {
191 printf("TFTP error occurred after missing %d responses",
192 tftp_err
.no_packets
);
193 } else if (rc
== -42) {
194 printf("TFTP error missing block %d, expected block was %d",
195 tftp_err
.blocks_missed
,
196 tftp_err
.blocks_received
);
202 static int net_load(char *buffer
, int len
)
208 memset(&fn_ip
, 0, sizeof(filename_ip_t
));
210 rc
= virtio_net_init(mac
);
212 puts("Could not initialize network device");
217 printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
218 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
220 set_mac_address(mac
); /* init ethernet layer */
223 rc
= dhcp(&fn_ip
, DEFAULT_BOOT_RETRIES
);
225 if (ip_version
== 4) {
226 set_ipv4_address(fn_ip
.own_ip
);
229 puts("Could not get IP address");
233 if (ip_version
== 4) {
234 printf(" Using IPv4 address: %d.%d.%d.%d\n",
235 (fn_ip
.own_ip
>> 24) & 0xFF, (fn_ip
.own_ip
>> 16) & 0xFF,
236 (fn_ip
.own_ip
>> 8) & 0xFF, fn_ip
.own_ip
& 0xFF);
237 } else if (ip_version
== 6) {
239 ipv6_to_str(fn_ip
.own_ip6
.addr
, ip6_str
);
240 printf(" Using IPv6 address: %s\n", ip6_str
);
244 printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
245 (fn_ip
.server_ip
>> 24) & 0xFF, (fn_ip
.server_ip
>> 16) & 0xFF,
246 (fn_ip
.server_ip
>> 8) & 0xFF, fn_ip
.server_ip
& 0xFF);
249 if (rc
== -4 || rc
== -3) {
250 puts("Can't obtain TFTP server IP address");
254 if (ip_version
== 4) {
255 printf(" Requesting file \"%s\" via TFTP from %d.%d.%d.%d\n",
257 (fn_ip
.server_ip
>> 24) & 0xFF, (fn_ip
.server_ip
>> 16) & 0xFF,
258 (fn_ip
.server_ip
>> 8) & 0xFF, fn_ip
.server_ip
& 0xFF);
259 } else if (ip_version
== 6) {
261 printf(" Requesting file \"%s\" via TFTP from ", fn_ip
.filename
);
262 ipv6_to_str(fn_ip
.server_ip6
.addr
, ip6_str
);
263 printf("%s\n", ip6_str
);
266 /* Do the TFTP load and print error message if necessary */
267 rc
= tftp_load(&fn_ip
, buffer
, len
, DEFAULT_TFTP_RETRIES
, ip_version
);
269 if (ip_version
== 4) {
270 dhcp_send_release(fn_ip
.fd
);
276 void panic(const char *string
)
284 static bool find_net_dev(Schib
*schib
, int dev_no
)
288 for (i
= 0; i
< 0x10000; i
++) {
289 net_schid
.sch_no
= i
;
290 r
= stsch_err(net_schid
, schib
);
291 if (r
== 3 || r
== -EIO
) {
294 if (!schib
->pmcw
.dnv
) {
297 if (!virtio_is_supported(net_schid
)) {
300 if (virtio_get_device_type() != VIRTIO_ID_NET
) {
303 if (dev_no
< 0 || schib
->pmcw
.dev
== dev_no
) {
311 static void virtio_setup(void)
319 * We unconditionally enable mss support. In every sane configuration,
320 * this will succeed; and even if it doesn't, stsch_err() can deal
321 * with the consequences.
323 enable_mss_facility();
325 if (store_iplb(&iplb
)) {
326 IPL_assert(iplb
.pbt
== S390_IPL_TYPE_CCW
, "IPL_TYPE_CCW expected");
327 dev_no
= iplb
.ccw
.devno
;
328 debug_print_int("device no. ", dev_no
);
329 net_schid
.ssid
= iplb
.ccw
.ssid
& 0x3;
330 debug_print_int("ssid ", net_schid
.ssid
);
331 found
= find_net_dev(&schib
, dev_no
);
333 for (ssid
= 0; ssid
< 0x3; ssid
++) {
334 net_schid
.ssid
= ssid
;
335 found
= find_net_dev(&schib
, -1);
342 IPL_assert(found
, "No virtio net device found");
350 sclp_print("Network boot starting...\n");
354 rc
= net_load(NULL
, (long)_start
);
356 sclp_print("Network loading done, starting kernel...\n");
357 asm volatile (" lpsw 0(%0) " : : "r"(0) : "memory");
360 panic("Failed to load OS from network\n");