-
Notifications
You must be signed in to change notification settings - Fork 1
/
0070-Support-UEFI-networking-protocols.patch
5052 lines (5007 loc) · 143 KB
/
0070-Support-UEFI-networking-protocols.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Michael Chang <[email protected]>
Date: Wed, 22 Feb 2017 14:27:50 +0800
Subject: [PATCH] Support UEFI networking protocols
References: fate#320130, bsc#1015589, bsc#1076132
Patch-Mainline: no
V1:
* Add preliminary support of UEFI networking protocols
* Support UEFI HTTPS Boot
V2:
* Workaround http data access in firmware
* Fix DNS device path parsing for efinet device
* Relaxed UEFI Protocol requirement
* Support Intel OPA (Omni-Path Architecture) PXE Boot
V3:
* Fix bufio in calculating address of next_buf
* Check HTTP respond code
* Use HEAD request method to test before GET
* Finish HTTP transaction in one go
* Fix bsc#1076132
Signed-off-by: Michael Chang <[email protected]>
[pjones: make efi_netfs not duplicate symbols from efinet]
Signed-off-by: Peter Jones <[email protected]>
---
grub-core/Makefile.core.def | 12 +
grub-core/io/bufio.c | 2 +-
grub-core/kern/efi/efi.c | 96 ++-
grub-core/net/drivers/efi/efinet.c | 27 +
grub-core/net/efi/dhcp.c | 397 ++++++++++
grub-core/net/efi/efi_netfs.c | 57 ++
grub-core/net/efi/http.c | 410 +++++++++++
grub-core/net/efi/ip4_config.c | 398 ++++++++++
grub-core/net/efi/ip6_config.c | 422 +++++++++++
grub-core/net/efi/net.c | 1428 ++++++++++++++++++++++++++++++++++++
grub-core/net/efi/pxe.c | 424 +++++++++++
grub-core/net/net.c | 74 ++
util/grub-mknetdir.c | 23 +-
include/grub/efi/api.h | 195 ++++-
include/grub/efi/dhcp.h | 343 +++++++++
include/grub/efi/http.h | 215 ++++++
include/grub/net/efi.h | 144 ++++
17 files changed, 4618 insertions(+), 49 deletions(-)
create mode 100644 grub-core/net/efi/dhcp.c
create mode 100644 grub-core/net/efi/efi_netfs.c
create mode 100644 grub-core/net/efi/http.c
create mode 100644 grub-core/net/efi/ip4_config.c
create mode 100644 grub-core/net/efi/ip6_config.c
create mode 100644 grub-core/net/efi/net.c
create mode 100644 grub-core/net/efi/pxe.c
create mode 100644 include/grub/efi/dhcp.h
create mode 100644 include/grub/efi/http.h
create mode 100644 include/grub/net/efi.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 6645fbae34f..56eb6a828e8 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2356,6 +2356,12 @@ module = {
common = hook/datehook.c;
};
+module = {
+ name = efi_netfs;
+ common = net/efi/efi_netfs.c;
+ enable = efi;
+};
+
module = {
name = net;
common = net/net.c;
@@ -2369,6 +2375,12 @@ module = {
common = net/ethernet.c;
common = net/arp.c;
common = net/netbuff.c;
+ efi = net/efi/net.c;
+ efi = net/efi/http.c;
+ efi = net/efi/pxe.c;
+ efi = net/efi/ip4_config.c;
+ efi = net/efi/ip6_config.c;
+ efi = net/efi/dhcp.c;
};
module = {
diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
index a458c3aca78..1637731535e 100644
--- a/grub-core/io/bufio.c
+++ b/grub-core/io/bufio.c
@@ -139,7 +139,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
return res;
/* Need to read some more. */
- next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
+ next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
/* Now read between file->offset + res and bufio->buffer_at. */
if (file->offset + res < next_buf)
{
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index 2bb8a0e7a38..bf16c4307bc 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -775,7 +775,7 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
{
grub_efi_ipv4_device_path_t *ipv4
= (grub_efi_ipv4_device_path_t *) dp;
- grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
+ grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x",
(unsigned) ipv4->local_ip_address[0],
(unsigned) ipv4->local_ip_address[1],
(unsigned) ipv4->local_ip_address[2],
@@ -788,33 +788,60 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
(unsigned) ipv4->remote_port,
(unsigned) ipv4->protocol,
(unsigned) ipv4->static_ip_address);
+ if (len == sizeof (*ipv4))
+ {
+ grub_printf (",%u.%u.%u.%u,%u.%u.%u.%u",
+ (unsigned) ipv4->gateway_ip_address[0],
+ (unsigned) ipv4->gateway_ip_address[1],
+ (unsigned) ipv4->gateway_ip_address[2],
+ (unsigned) ipv4->gateway_ip_address[3],
+ (unsigned) ipv4->subnet_mask[0],
+ (unsigned) ipv4->subnet_mask[1],
+ (unsigned) ipv4->subnet_mask[2],
+ (unsigned) ipv4->subnet_mask[3]);
+ }
+ grub_printf (")");
}
break;
case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
{
grub_efi_ipv6_device_path_t *ipv6
= (grub_efi_ipv6_device_path_t *) dp;
- grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
- (unsigned) ipv6->local_ip_address[0],
- (unsigned) ipv6->local_ip_address[1],
- (unsigned) ipv6->local_ip_address[2],
- (unsigned) ipv6->local_ip_address[3],
- (unsigned) ipv6->local_ip_address[4],
- (unsigned) ipv6->local_ip_address[5],
- (unsigned) ipv6->local_ip_address[6],
- (unsigned) ipv6->local_ip_address[7],
- (unsigned) ipv6->remote_ip_address[0],
- (unsigned) ipv6->remote_ip_address[1],
- (unsigned) ipv6->remote_ip_address[2],
- (unsigned) ipv6->remote_ip_address[3],
- (unsigned) ipv6->remote_ip_address[4],
- (unsigned) ipv6->remote_ip_address[5],
- (unsigned) ipv6->remote_ip_address[6],
- (unsigned) ipv6->remote_ip_address[7],
+ grub_printf ("/IPv6(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x,%u,%u,%x,%x",
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->local_ip_address[7]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->remote_ip_address[7]),
(unsigned) ipv6->local_port,
(unsigned) ipv6->remote_port,
(unsigned) ipv6->protocol,
(unsigned) ipv6->static_ip_address);
+ if (len == sizeof (*ipv6))
+ {
+ grub_printf (",%u,%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
+ (unsigned) ipv6->prefix_length,
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[0]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[1]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[2]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[3]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[4]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[5]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[6]),
+ (unsigned) grub_be_to_cpu16 (ipv6->gateway_ip_address[7]));
+ }
+ grub_printf (")");
}
break;
case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
@@ -861,6 +888,39 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
dump_vendor_path ("Messaging",
(grub_efi_vendor_device_path_t *) dp);
break;
+ case GRUB_EFI_URI_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_uri_device_path_t *uri
+ = (grub_efi_uri_device_path_t *) dp;
+ grub_printf ("/URI(%s)", uri->uri);
+ }
+ break;
+ case GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE:
+ {
+ grub_efi_dns_device_path_t *dns
+ = (grub_efi_dns_device_path_t *) dp;
+ if (dns->is_ipv6)
+ {
+ grub_printf ("/DNS(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x)",
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[0])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[1])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[2])),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3]) >> 16),
+ (grub_uint16_t)(grub_be_to_cpu32(dns->dns_server_ip[0].addr[3])));
+ }
+ else
+ {
+ grub_printf ("/DNS(%d.%d.%d.%d)",
+ dns->dns_server_ip[0].v4.addr[0],
+ dns->dns_server_ip[0].v4.addr[1],
+ dns->dns_server_ip[0].v4.addr[2],
+ dns->dns_server_ip[0].v4.addr[3]);
+ }
+ }
+ break;
default:
grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
break;
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
index a91df09ee6d..225dc896da0 100644
--- a/grub-core/net/drivers/efi/efinet.c
+++ b/grub-core/net/drivers/efi/efinet.c
@@ -27,6 +27,7 @@
#include <grub/lib/hexdump.h>
#include <grub/types.h>
#include <grub/net/netbuff.h>
+#include <grub/env.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -508,6 +509,17 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
ldp = grub_efi_find_last_device_path (ddp);
+ /* Skip the DNS Device */
+ if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
+ && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
+ {
+ ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ ldp->length = sizeof (*ldp);
+
+ ldp = grub_efi_find_last_device_path (ddp);
+ }
+
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
@@ -781,6 +793,7 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
+ && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
continue;
dup_dp = grub_efi_duplicate_device_path (dp);
@@ -795,6 +808,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
dup_ldp->length = sizeof (*dup_ldp);
}
+ dup_ldp = grub_efi_find_last_device_path (dup_dp);
+ if (GRUB_EFI_DEVICE_PATH_SUBTYPE (dup_ldp) == GRUB_EFI_DNS_DEVICE_PATH_SUBTYPE)
+ {
+ dup_ldp = grub_efi_find_last_device_path (dup_dp);
+ dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
+ dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
+ dup_ldp->length = sizeof (*dup_ldp);
+ }
+
dup_ldp = grub_efi_find_last_device_path (dup_dp);
dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
@@ -889,6 +911,9 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
GRUB_MOD_INIT(efinet)
{
+ if (grub_efi_net_config)
+ return;
+
grub_efinet_findcards ();
grub_efi_net_config = grub_efi_net_config_real;
}
@@ -900,5 +925,7 @@ GRUB_MOD_FINI(efinet)
FOR_NET_CARDS_SAFE (card, next)
if (card->driver == &efidriver)
grub_net_card_unregister (card);
+
+ grub_efi_net_config = NULL;
}
diff --git a/grub-core/net/efi/dhcp.c b/grub-core/net/efi/dhcp.c
new file mode 100644
index 00000000000..ca19902441b
--- /dev/null
+++ b/grub-core/net/efi/dhcp.c
@@ -0,0 +1,397 @@
+#include <grub/mm.h>
+#include <grub/command.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+
+#ifdef GRUB_EFI_NET_DEBUG
+static void
+dhcp4_mode_print (grub_efi_dhcp4_mode_data_t *mode)
+{
+ switch (mode->state)
+ {
+ case GRUB_EFI_DHCP4_STOPPED:
+ grub_printf ("STATE: STOPPED\n");
+ break;
+ case GRUB_EFI_DHCP4_INIT:
+ grub_printf ("STATE: INIT\n");
+ break;
+ case GRUB_EFI_DHCP4_SELECTING:
+ grub_printf ("STATE: SELECTING\n");
+ break;
+ case GRUB_EFI_DHCP4_REQUESTING:
+ grub_printf ("STATE: REQUESTING\n");
+ break;
+ case GRUB_EFI_DHCP4_BOUND:
+ grub_printf ("STATE: BOUND\n");
+ break;
+ case GRUB_EFI_DHCP4_RENEWING:
+ grub_printf ("STATE: RENEWING\n");
+ break;
+ case GRUB_EFI_DHCP4_REBINDING:
+ grub_printf ("STATE: REBINDING\n");
+ break;
+ case GRUB_EFI_DHCP4_INIT_REBOOT:
+ grub_printf ("STATE: INIT_REBOOT\n");
+ break;
+ case GRUB_EFI_DHCP4_REBOOTING:
+ grub_printf ("STATE: REBOOTING\n");
+ break;
+ default:
+ grub_printf ("STATE: UNKNOWN\n");
+ break;
+ }
+
+ grub_printf ("CLIENT_ADDRESS: %u.%u.%u.%u\n",
+ mode->client_address[0],
+ mode->client_address[1],
+ mode->client_address[2],
+ mode->client_address[3]);
+ grub_printf ("SERVER_ADDRESS: %u.%u.%u.%u\n",
+ mode->server_address[0],
+ mode->server_address[1],
+ mode->server_address[2],
+ mode->server_address[3]);
+ grub_printf ("SUBNET_MASK: %u.%u.%u.%u\n",
+ mode->subnet_mask[0],
+ mode->subnet_mask[1],
+ mode->subnet_mask[2],
+ mode->subnet_mask[3]);
+ grub_printf ("ROUTER_ADDRESS: %u.%u.%u.%u\n",
+ mode->router_address[0],
+ mode->router_address[1],
+ mode->router_address[2],
+ mode->router_address[3]);
+}
+#endif
+
+static grub_efi_ipv4_address_t *
+grub_efi_dhcp4_parse_dns (grub_efi_dhcp4_protocol_t *dhcp4, grub_efi_dhcp4_packet_t *reply_packet)
+{
+ grub_efi_dhcp4_packet_option_t **option_list;
+ grub_efi_status_t status;
+ grub_efi_uint32_t option_count = 0;
+ grub_efi_uint32_t i;
+
+ status = dhcp4->parse(dhcp4, reply_packet, &option_count, NULL);
+
+ if (status != GRUB_EFI_BUFFER_TOO_SMALL)
+ return NULL;
+
+ option_list = grub_malloc (option_count * sizeof(*option_list));
+ if (!option_list)
+ return NULL;
+
+ status = dhcp4->parse(dhcp4, reply_packet, &option_count, option_list);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (option_list);
+ return NULL;
+ }
+
+ for (i = 0; i < option_count; ++i)
+ {
+ if (option_list[i]->op_code == 6)
+ {
+ grub_efi_ipv4_address_t *dns_address;
+
+ if (((option_list[i]->length & 0x3) != 0) || (option_list[i]->length == 0))
+ continue;
+
+ /* We only contact primary dns */
+ dns_address = grub_malloc (sizeof (*dns_address));
+ if (!dns_address)
+ {
+ grub_free (option_list);
+ return NULL;
+ }
+ grub_memcpy (dns_address, option_list[i]->data, sizeof (dns_address));
+ grub_free (option_list);
+ return dns_address;
+ }
+ }
+
+ grub_free (option_list);
+ return NULL;
+}
+
+#if 0
+/* Somehow this doesn't work ... */
+static grub_err_t
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char **args __attribute__ ((unused)))
+{
+ struct grub_efi_net_device *dev;
+ for (dev = net_devices; dev; dev = dev->next)
+ {
+ grub_efi_pxe_t *pxe = dev->ip4_pxe;
+ grub_efi_pxe_mode_t *mode = pxe->mode;
+ grub_efi_status_t status;
+
+ if (!mode->started)
+ {
+ status = pxe->start(pxe, 0);
+
+ if (status != GRUB_EFI_SUCCESS)
+ grub_printf ("Couldn't start PXE\n");
+ }
+
+ status = pxe->dhcp(pxe, 0);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 configure failed, %d\n", (int)status);
+ continue;
+ }
+
+ dev->prefer_ip6 = 0;
+ }
+
+ return GRUB_ERR_NONE;
+}
+#endif
+
+static grub_err_t
+grub_cmd_efi_bootp (struct grub_command *cmd __attribute__ ((unused)),
+ int argc,
+ char **args)
+{
+ struct grub_efi_net_device *netdev;
+
+ for (netdev = net_devices; netdev; netdev = netdev->next)
+ {
+ grub_efi_status_t status;
+ grub_efi_dhcp4_mode_data_t mode;
+ grub_efi_dhcp4_config_data_t config;
+ grub_efi_dhcp4_packet_option_t *options;
+ grub_efi_ipv4_address_t *dns_address;
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_net_ip_address_t ip_addr;
+ grub_efi_net_interface_t *inf = NULL;
+
+ if (argc > 0 && grub_strcmp (netdev->card_name, args[0]) != 0)
+ continue;
+
+ grub_memset (&config, 0, sizeof(config));
+
+ config.option_count = 1;
+ options = grub_malloc (sizeof(*options) + 2);
+ /* Parameter request list */
+ options->op_code = 55;
+ options->length = 3;
+ /* subnet mask */
+ options->data[0] = 1;
+ /* router */
+ options->data[1] = 3;
+ /* DNS */
+ options->data[2] = 6;
+ config.option_list = &options;
+
+ /* FIXME: What if the dhcp has bounded */
+ status = netdev->dhcp4->configure(netdev->dhcp4, &config);
+ grub_free (options);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 configure failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = netdev->dhcp4->start(netdev->dhcp4, NULL);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 start failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = netdev->dhcp4->get_mode_data(netdev->dhcp4, &mode);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
+ continue;
+ }
+
+#ifdef GRUB_EFI_NET_DEBUG
+ dhcp4_mode_print (&mode);
+#endif
+
+ for (inf = netdev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6 == 0)
+ break;
+
+ grub_memcpy (net_ip.ip4.address, mode.client_address, sizeof (net_ip.ip4.address));
+ grub_memcpy (net_ip.ip4.subnet_mask, mode.subnet_mask, sizeof (net_ip.ip4.subnet_mask));
+
+ if (!inf)
+ {
+ char *name = grub_xasprintf ("%s:dhcp", netdev->card_name);
+
+ net_ip.is_ip6 = 0;
+ inf = grub_efi_net_create_interface (netdev,
+ name,
+ &net_ip,
+ 1);
+ grub_free (name);
+ }
+ else
+ {
+ efi_net_interface_set_address (inf, &net_ip, 1);
+ }
+
+ grub_memcpy (ip_addr.ip4, mode.router_address, sizeof (ip_addr.ip4));
+ efi_net_interface_set_gateway (inf, &ip_addr);
+
+ dns_address = grub_efi_dhcp4_parse_dns (netdev->dhcp4, mode.reply_packet);
+ if (dns_address)
+ efi_net_interface_set_dns (inf, (grub_efi_net_ip_address_t *)&dns_address);
+
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+
+static grub_err_t
+grub_cmd_efi_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
+ int argc,
+ char **args)
+{
+ struct grub_efi_net_device *dev;
+ grub_efi_uint32_t ia_id;
+
+ for (dev = net_devices, ia_id = 0; dev; dev = dev->next, ia_id++)
+ {
+ grub_efi_dhcp6_config_data_t config;
+ grub_efi_dhcp6_packet_option_t *option_list[1];
+ grub_efi_dhcp6_packet_option_t *opt;
+ grub_efi_status_t status;
+ grub_efi_dhcp6_mode_data_t mode;
+ grub_efi_dhcp6_retransmission_t retrans;
+ grub_efi_net_ip_manual_address_t net_ip;
+ grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+ grub_efi_net_interface_t *inf = NULL;
+
+ if (argc > 0 && grub_strcmp (dev->card_name, args[0]) != 0)
+ continue;
+
+ opt = grub_malloc (sizeof(*opt) + 2 * sizeof (grub_efi_uint16_t));
+
+#define GRUB_EFI_DHCP6_OPT_ORO 6
+
+ opt->op_code = grub_cpu_to_be16_compile_time (GRUB_EFI_DHCP6_OPT_ORO);
+ opt->op_len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_efi_uint16_t));
+
+#define GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL 59
+#define GRUB_EFI_DHCP6_OPT_DNS_SERVERS 23
+
+ grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_BOOT_FILE_URL));
+ grub_set_unaligned16 (opt->data + 1 * sizeof (grub_efi_uint16_t),
+ grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS));
+
+ option_list[0] = opt;
+ retrans.irt = 4;
+ retrans.mrc = 4;
+ retrans.mrt = 32;
+ retrans.mrd = 60;
+
+ config.dhcp6_callback = NULL;
+ config.callback_context = NULL;
+ config.option_count = 1;
+ config.option_list = option_list;
+ config.ia_descriptor.ia_id = ia_id;
+ config.ia_descriptor.type = GRUB_EFI_DHCP6_IA_TYPE_NA;
+ config.ia_info_event = NULL;
+ config.reconfigure_accept = 0;
+ config.rapid_commit = 0;
+ config.solicit_retransmission = &retrans;
+
+ status = dev->dhcp6->configure(dev->dhcp6, &config);
+ grub_free (opt);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp6 configure failed, %d\n", (int)status);
+ continue;
+ }
+ status = dev->dhcp6->start(dev->dhcp6);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp6 start failed, %d\n", (int)status);
+ continue;
+ }
+
+ status = dev->dhcp6->get_mode_data(dev->dhcp6, &mode, NULL);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("dhcp4 get mode failed, %d\n", (int)status);
+ continue;
+ }
+
+ for (inf = dev->net_interfaces; inf; inf = inf->next)
+ if (inf->prefer_ip6 == 1)
+ break;
+
+ grub_memcpy (net_ip.ip6.address, mode.ia->ia_address[0].ip_address, sizeof (net_ip.ip6.address));
+ net_ip.ip6.prefix_length = 64;
+ net_ip.ip6.is_anycast = 0;
+ net_ip.is_ip6 = 1;
+
+ if (!inf)
+ {
+ char *name = grub_xasprintf ("%s:dhcp", dev->card_name);
+
+ inf = grub_efi_net_create_interface (dev,
+ name,
+ &net_ip,
+ 1);
+ grub_free (name);
+ }
+ else
+ {
+ efi_net_interface_set_address (inf, &net_ip, 1);
+ }
+
+ {
+ grub_efi_uint32_t count = 0;
+ grub_efi_dhcp6_packet_option_t **options = NULL;
+ grub_efi_uint32_t i;
+
+ status = dev->dhcp6->parse(dev->dhcp6, mode.ia->reply_packet, &count, NULL);
+
+ if (status == GRUB_EFI_BUFFER_TOO_SMALL && count)
+ {
+ options = grub_malloc (count * sizeof(*options));
+ status = dev->dhcp6->parse(dev->dhcp6, mode.ia->reply_packet, &count, options);
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ if (options)
+ grub_free (options);
+ continue;
+ }
+
+ for (i = 0; i < count; ++i)
+ {
+ if (options[i]->op_code == grub_cpu_to_be16_compile_time(GRUB_EFI_DHCP6_OPT_DNS_SERVERS))
+ {
+ grub_efi_net_ip_address_t dns;
+ grub_memcpy (dns.ip6, options[i]->data, sizeof(net_ip.ip6));
+ efi_net_interface_set_dns (inf, &dns);
+ break;
+ }
+ }
+
+ if (options)
+ grub_free (options);
+ }
+
+ b->free_pool(mode.client_id);
+ b->free_pool(mode.ia);
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+grub_command_func_t grub_efi_net_bootp = grub_cmd_efi_bootp;
+grub_command_func_t grub_efi_net_bootp6 = grub_cmd_efi_bootp6;
diff --git a/grub-core/net/efi/efi_netfs.c b/grub-core/net/efi/efi_netfs.c
new file mode 100644
index 00000000000..ef371d885ea
--- /dev/null
+++ b/grub-core/net/efi/efi_netfs.c
@@ -0,0 +1,57 @@
+#include <grub/dl.h>
+#include <grub/env.h>
+#define EFI_NET_CMD_PREFIX "net_efi"
+#include <grub/net/efi.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_command_t cmd_efi_lsroutes;
+static grub_command_t cmd_efi_lscards;
+static grub_command_t cmd_efi_lsaddrs;
+static grub_command_t cmd_efi_addaddr;
+static grub_command_t cmd_efi_bootp;
+static grub_command_t cmd_efi_bootp6;
+
+static int initialized;
+
+GRUB_MOD_INIT(efi_netfs)
+{
+ if (grub_net_open)
+ return;
+
+ if (grub_efi_net_fs_init ())
+ {
+ cmd_efi_lsroutes = grub_register_command ("net_efi_ls_routes", grub_efi_net_list_routes,
+ "", N_("list network routes"));
+ cmd_efi_lscards = grub_register_command ("net_efi_ls_cards", grub_efi_net_list_cards,
+ "", N_("list network cards"));
+ cmd_efi_lsaddrs = grub_register_command ("net_efi_ls_addr", grub_efi_net_list_addrs,
+ "", N_("list network addresses"));
+ cmd_efi_addaddr = grub_register_command ("net_efi_add_addr", grub_efi_net_add_addr,
+ N_("SHORTNAME CARD ADDRESS [HWADDRESS]"),
+ N_("Add a network address."));
+ cmd_efi_bootp = grub_register_command ("net_efi_bootp", grub_efi_net_bootp,
+ N_("[CARD]"),
+ N_("perform a bootp autoconfiguration"));
+ cmd_efi_bootp6 = grub_register_command ("net_efi_bootp6", grub_efi_net_bootp6,
+ N_("[CARD]"),
+ N_("perform a bootp autoconfiguration"));
+ initialized = 1;
+ }
+}
+
+GRUB_MOD_FINI(efi_netfs)
+{
+ if (initialized)
+ {
+ grub_unregister_command (cmd_efi_lsroutes);
+ grub_unregister_command (cmd_efi_lscards);
+ grub_unregister_command (cmd_efi_lsaddrs);
+ grub_unregister_command (cmd_efi_addaddr);
+ grub_unregister_command (cmd_efi_bootp);
+ grub_unregister_command (cmd_efi_bootp6);
+ grub_efi_net_fs_fini ();
+ initialized = 0;
+ return;
+ }
+}
diff --git a/grub-core/net/efi/http.c b/grub-core/net/efi/http.c
new file mode 100644
index 00000000000..de28badccb0
--- /dev/null
+++ b/grub-core/net/efi/http.c
@@ -0,0 +1,410 @@
+
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+#include <grub/net/efi.h>
+#include <grub/charset.h>
+
+static void
+http_configure (struct grub_efi_net_device *dev, int prefer_ip6)
+{
+ grub_efi_http_config_data_t http_config;
+ grub_efi_httpv4_access_point_t httpv4_node;
+ grub_efi_httpv6_access_point_t httpv6_node;
+ grub_efi_status_t status;
+
+ grub_efi_http_t *http = dev->http;
+
+ grub_memset (&http_config, 0, sizeof(http_config));
+ http_config.http_version = GRUB_EFI_HTTPVERSION11;
+ http_config.timeout_millisec = 5000;
+
+ if (prefer_ip6)
+ {
+ grub_efi_uintn_t sz;
+ grub_efi_ip6_config_manual_address_t manual_address;
+
+ http_config.local_address_is_ipv6 = 1;
+ sz = sizeof (manual_address);
+ status = dev->ip6_config->get_data(dev->ip6_config,
+ GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
+ &sz, &manual_address);
+
+ if (status == GRUB_EFI_NOT_FOUND)
+ {
+ grub_printf ("The MANUAL ADDRESS is not found\n");
+ }
+
+ /* FIXME: The manual interface would return BUFFER TOO SMALL !!! */
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_printf ("??? %d\n",(int) status);
+ return;
+ }
+
+ grub_memcpy (httpv6_node.local_address, manual_address.address, sizeof (httpv6_node.local_address));
+ httpv6_node.local_port = 0;
+ http_config.access_point.ipv6_node = &httpv6_node;
+ }
+ else
+ {
+ http_config.local_address_is_ipv6 = 0;
+ grub_memset (&httpv4_node, 0, sizeof(httpv4_node));
+ httpv4_node.use_default_address = 1;
+
+ /* Use random port here */
+ /* See TcpBind() in edk2/NetworkPkg/TcpDxe/TcpDispatcher.c */
+ httpv4_node.local_port = 0;
+ http_config.access_point.ipv4_node = &httpv4_node;
+ }
+
+ status = http->configure(http, &http_config);
+
+ if (status == GRUB_EFI_ALREADY_STARTED)
+ {
+ /* XXX: This hangs HTTPS boot */
+#if 0
+ if (http->configure(http, NULL) != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_IO, N_("couldn't reset http instance"));
+ grub_print_error ();
+ return;
+ }
+ status = http->configure(http, &http_config);
+#endif
+ return;
+ }
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_error (GRUB_ERR_IO, N_("couldn't configure http protocol, reason: %d"), (int)status);
+ grub_print_error ();
+ return ;
+ }
+}
+
+static grub_efi_boolean_t request_callback_done;
+static grub_efi_boolean_t response_callback_done;
+
+static void
+grub_efi_http_request_callback (grub_efi_event_t event __attribute__ ((unused)),
+ void *context __attribute__ ((unused)))
+{
+ request_callback_done = 1;
+}
+
+static void
+grub_efi_http_response_callback (grub_efi_event_t event __attribute__ ((unused)),
+ void *context __attribute__ ((unused)))
+{
+ response_callback_done = 1;
+}
+
+static grub_err_t
+efihttp_request (grub_efi_http_t *http, char *server, char *name, int use_https, int headeronly, grub_off_t *file_size)
+{
+ grub_efi_http_request_data_t request_data;
+ grub_efi_http_message_t request_message;
+ grub_efi_http_token_t request_token;
+ grub_efi_http_response_data_t response_data;
+ grub_efi_http_message_t response_message;
+ grub_efi_http_token_t response_token;
+ grub_efi_http_header_t request_headers[3];
+
+ grub_efi_status_t status;
+ grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
+ char *url = NULL;
+
+ request_headers[0].field_name = (grub_efi_char8_t *)"Host";
+ request_headers[0].field_value = (grub_efi_char8_t *)server;
+ request_headers[1].field_name = (grub_efi_char8_t *)"Accept";
+ request_headers[1].field_value = (grub_efi_char8_t *)"*/*";
+ request_headers[2].field_name = (grub_efi_char8_t *)"User-Agent";
+ request_headers[2].field_value = (grub_efi_char8_t *)"UefiHttpBoot/1.0";
+
+ {
+ grub_efi_ipv6_address_t address;
+ const char *rest;
+ grub_efi_char16_t *ucs2_url;
+ grub_size_t url_len, ucs2_url_len;
+ const char *protocol = (use_https == 1) ? "https" : "http";
+
+ if (grub_efi_string_to_ip6_address (server, &address, &rest) && *rest == 0)
+ url = grub_xasprintf ("%s://[%s]%s", protocol, server, name);
+ else
+ url = grub_xasprintf ("%s://%s%s", protocol, server, name);
+
+ if (!url)
+ {
+ return grub_errno;
+ }
+
+ url_len = grub_strlen (url);
+ ucs2_url_len = url_len * GRUB_MAX_UTF16_PER_UTF8;
+ ucs2_url = grub_malloc ((ucs2_url_len + 1) * sizeof (ucs2_url[0]));
+
+ if (!ucs2_url)
+ {
+ grub_free (url);
+ return grub_errno;
+ }
+
+ ucs2_url_len = grub_utf8_to_utf16 (ucs2_url, ucs2_url_len, (grub_uint8_t *)url, url_len, NULL); /* convert string format from ascii to usc2 */
+ ucs2_url[ucs2_url_len] = 0;
+ grub_free (url);
+ request_data.url = ucs2_url;
+ }
+
+ request_data.method = (headeronly > 0) ? GRUB_EFI_HTTPMETHODHEAD : GRUB_EFI_HTTPMETHODGET;
+
+ request_message.data.request = &request_data;
+ request_message.header_count = 3;
+ request_message.headers = request_headers;
+ request_message.body_length = 0;
+ request_message.body = NULL;
+
+ /* request token */
+ request_token.event = NULL;
+ request_token.status = GRUB_EFI_NOT_READY;
+ request_token.message = &request_message;
+
+ request_callback_done = 0;
+ status = b->create_event(GRUB_EFI_EVT_NOTIFY_SIGNAL, GRUB_EFI_TPL_CALLBACK,
+ grub_efi_http_request_callback, NULL,
+ &request_token.event);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%x\n", status);
+ }
+
+ status = http->request(http, &request_token);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event(request_token.event);
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to send a request! status=0x%x\n", status);
+ }
+ /* TODO: Add Timeout */
+ while (!request_callback_done)
+ http->poll(http);
+
+ response_data.status_code = GRUB_EFI_HTTP_STATUS_UNSUPPORTED_STATUS;
+ response_message.data.response = &response_data;
+ /* herader_count will be updated by the HTTP driver on response */
+ response_message.header_count = 0;
+ /* headers will be populated by the driver on response */
+ response_message.headers = NULL;
+ /* use zero BodyLength to only receive the response headers */
+ response_message.body_length = 0;
+ response_message.body = NULL;
+ response_token.event = NULL;
+
+ status = b->create_event(GRUB_EFI_EVT_NOTIFY_SIGNAL, GRUB_EFI_TPL_CALLBACK,
+ grub_efi_http_response_callback, NULL,
+ &response_token.event);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ b->close_event(request_token.event);
+ grub_free (request_data.url);
+ return grub_error (GRUB_ERR_IO, "Fail to create an event! status=0x%x\n", status);
+ }
+
+ response_token.status = GRUB_EFI_SUCCESS;
+ response_token.message = &response_message;
+
+ /* wait for HTTP response */
+ response_callback_done = 0;
+ status = http->response(http, &response_token);
+
+ if (status != GRUB_EFI_SUCCESS)
+ {