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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
|
/** @file
Boot functions implementation for UefiPxeBc Driver.
Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "PxeBcImpl.h"
/**
Display the string of the boot item.
If the length of the boot item string beyond 70 Char, just display 70 Char.
@param[in] Str The pointer to the string.
@param[in] Len The length of the string.
**/
VOID
PxeBcDisplayBootItem (
IN UINT8 *Str,
IN UINT8 Len
)
{
UINT8 Tmp;
//
// Cut off the chars behind 70th.
//
Len = (UINT8)MIN (PXEBC_DISPLAY_MAX_LINE, Len);
Tmp = Str[Len];
Str[Len] = 0;
AsciiPrint ("%a \n", Str);
//
// Restore the original 70th char.
//
Str[Len] = Tmp;
}
/**
Select and maintain the boot prompt if needed.
@param[in] Private Pointer to PxeBc private data.
@retval EFI_SUCCESS Selected boot prompt done.
@retval EFI_TIMEOUT Selected boot prompt timed out.
@retval EFI_NOT_FOUND The proxy offer is not Pxe10.
@retval EFI_ABORTED User cancelled the operation.
@retval EFI_NOT_READY Reading the input key from the keyboard has not finish.
**/
EFI_STATUS
PxeBcSelectBootPrompt (
IN PXEBC_PRIVATE_DATA *Private
)
{
PXEBC_DHCP_PACKET_CACHE *Cache;
PXEBC_VENDOR_OPTION *VendorOpt;
EFI_PXE_BASE_CODE_MODE *Mode;
EFI_EVENT TimeoutEvent;
EFI_EVENT DescendEvent;
EFI_INPUT_KEY InputKey;
EFI_STATUS Status;
UINT32 OfferType;
UINT8 Timeout;
UINT8 *Prompt;
UINT8 PromptLen;
INT32 SecCol;
INT32 SecRow;
TimeoutEvent = NULL;
DescendEvent = NULL;
Mode = Private->PxeBc.Mode;
Cache = Mode->ProxyOfferReceived ? &Private->ProxyOffer : &Private->DhcpAck;
OfferType = Mode->UsingIpv6 ? Cache->Dhcp6.OfferType : Cache->Dhcp4.OfferType;
//
// Only DhcpPxe10 and ProxyPxe10 offer needs boot prompt.
//
if ((OfferType != PxeOfferTypeProxyPxe10) && (OfferType != PxeOfferTypeDhcpPxe10)) {
return EFI_NOT_FOUND;
}
//
// There is no specified ProxyPxe10 for IPv6 in PXE and UEFI spec.
//
ASSERT (!Mode->UsingIpv6);
VendorOpt = &Cache->Dhcp4.VendorOpt;
//
// According to the PXE specification 2.1, Table 2-1 PXE DHCP Options,
// we must not consider a boot prompt or boot menu if all of the following hold:
// - the PXE_DISCOVERY_CONTROL tag(6) is present inside the Vendor Options(43), and has bit 3 set
// - a boot file name has been presented in the initial DHCP or ProxyDHCP offer packet.
//
if (IS_DISABLE_PROMPT_MENU (VendorOpt->DiscoverCtrl) &&
(Cache->Dhcp4.OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL))
{
return EFI_ABORTED;
}
if (!IS_VALID_BOOT_PROMPT (VendorOpt->BitMap)) {
return EFI_TIMEOUT;
}
Timeout = VendorOpt->MenuPrompt->Timeout;
Prompt = VendorOpt->MenuPrompt->Prompt;
PromptLen = (UINT8)(VendorOpt->MenuPromptLen - 1);
//
// The valid scope of Timeout refers to PXE2.1 spec.
//
if (Timeout == 0) {
return EFI_TIMEOUT;
}
if (Timeout == 255) {
return EFI_SUCCESS;
}
//
// Create and start a timer as timeout event.
//
Status = gBS->CreateEvent (
EVT_TIMER,
TPL_CALLBACK,
NULL,
NULL,
&TimeoutEvent
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gBS->SetTimer (
TimeoutEvent,
TimerRelative,
MultU64x32 (Timeout, TICKS_PER_SECOND)
);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
}
//
// Create and start a periodic timer as descend event by second.
//
Status = gBS->CreateEvent (
EVT_TIMER,
TPL_CALLBACK,
NULL,
NULL,
&DescendEvent
);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
}
Status = gBS->SetTimer (
DescendEvent,
TimerPeriodic,
TICKS_PER_SECOND
);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
}
//
// Display the boot item and cursor on the screen.
//
SecCol = gST->ConOut->Mode->CursorColumn;
SecRow = gST->ConOut->Mode->CursorRow;
PxeBcDisplayBootItem (Prompt, PromptLen);
gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);
AsciiPrint ("(%d) ", Timeout--);
Status = EFI_TIMEOUT;
while (EFI_ERROR (gBS->CheckEvent (TimeoutEvent))) {
if (!EFI_ERROR (gBS->CheckEvent (DescendEvent))) {
gST->ConOut->SetCursorPosition (gST->ConOut, SecCol + PromptLen, SecRow);
AsciiPrint ("(%d) ", Timeout--);
}
if (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {
gBS->Stall (10 * TICKS_PER_MS);
continue;
}
//
// Parse the input key by user.
// If <F8> or <Ctrl> + <M> is pressed, return success to display the boot menu.
//
if (InputKey.ScanCode == 0) {
switch (InputKey.UnicodeChar) {
case CTRL ('c'):
Status = EFI_ABORTED;
break;
case CTRL ('m'):
case 'm':
case 'M':
Status = EFI_SUCCESS;
break;
default:
continue;
}
} else {
switch (InputKey.ScanCode) {
case SCAN_F8:
Status = EFI_SUCCESS;
break;
case SCAN_ESC:
Status = EFI_ABORTED;
break;
default:
continue;
}
}
break;
}
//
// Reset the cursor on the screen.
//
gST->ConOut->SetCursorPosition (gST->ConOut, 0, SecRow + 1);
ON_EXIT:
if (DescendEvent != NULL) {
gBS->CloseEvent (DescendEvent);
}
if (TimeoutEvent != NULL) {
gBS->CloseEvent (TimeoutEvent);
}
return Status;
}
/**
Select the boot menu by user's input.
@param[in] Private Pointer to PxeBc private data.
@param[out] Type The type of the menu.
@param[in] UseDefaultItem Use default item or not.
@retval EFI_ABORTED User cancel operation.
@retval EFI_SUCCESS Select the boot menu success.
@retval EFI_NOT_READY Read the input key from the keyboard has not finish.
**/
EFI_STATUS
PxeBcSelectBootMenu (
IN PXEBC_PRIVATE_DATA *Private,
OUT UINT16 *Type,
IN BOOLEAN UseDefaultItem
)
{
EFI_PXE_BASE_CODE_MODE *Mode;
PXEBC_DHCP_PACKET_CACHE *Cache;
PXEBC_VENDOR_OPTION *VendorOpt;
EFI_INPUT_KEY InputKey;
UINT32 OfferType;
UINT8 MenuSize;
UINT8 MenuNum;
INT32 TopRow;
UINT16 Select;
UINT16 LastSelect;
UINT8 Index;
BOOLEAN Finish;
CHAR8 Blank[PXEBC_DISPLAY_MAX_LINE];
PXEBC_BOOT_MENU_ENTRY *MenuItem;
PXEBC_BOOT_MENU_ENTRY *MenuArray[PXEBC_MENU_MAX_NUM];
Finish = FALSE;
Select = 0;
Index = 0;
*Type = 0;
Mode = Private->PxeBc.Mode;
Cache = Mode->ProxyOfferReceived ? &Private->ProxyOffer : &Private->DhcpAck;
OfferType = Mode->UsingIpv6 ? Cache->Dhcp6.OfferType : Cache->Dhcp4.OfferType;
//
// There is no specified DhcpPxe10/ProxyPxe10 for IPv6 in PXE and UEFI spec.
//
ASSERT (!Mode->UsingIpv6);
ASSERT (OfferType == PxeOfferTypeProxyPxe10 || OfferType == PxeOfferTypeDhcpPxe10);
VendorOpt = &Cache->Dhcp4.VendorOpt;
if (!IS_VALID_BOOT_MENU (VendorOpt->BitMap)) {
return EFI_SUCCESS;
}
//
// Display the boot menu on the screen.
//
SetMem (Blank, sizeof (Blank), ' ');
MenuSize = VendorOpt->BootMenuLen;
MenuItem = VendorOpt->BootMenu;
if (MenuSize == 0) {
return EFI_DEVICE_ERROR;
}
while (MenuSize > 0 && Index < PXEBC_MENU_MAX_NUM) {
ASSERT (MenuItem != NULL);
MenuArray[Index] = MenuItem;
MenuSize = (UINT8)(MenuSize - (MenuItem->DescLen + 3));
MenuItem = (PXEBC_BOOT_MENU_ENTRY *)((UINT8 *)MenuItem + MenuItem->DescLen + 3);
Index++;
}
if (UseDefaultItem) {
ASSERT (MenuArray[0] != NULL);
CopyMem (Type, &MenuArray[0]->Type, sizeof (UINT16));
*Type = NTOHS (*Type);
return EFI_SUCCESS;
}
MenuNum = Index;
for (Index = 0; Index < MenuNum; Index++) {
ASSERT (MenuArray[Index] != NULL);
PxeBcDisplayBootItem (MenuArray[Index]->DescStr, MenuArray[Index]->DescLen);
}
TopRow = gST->ConOut->Mode->CursorRow - MenuNum;
//
// Select the boot item by user in the boot menu.
//
do {
//
// Highlight selected row.
//
gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_BLACK, EFI_LIGHTGRAY));
gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + Select);
ASSERT (Select < PXEBC_MENU_MAX_NUM);
ASSERT (MenuArray[Select] != NULL);
Blank[MenuArray[Select]->DescLen] = 0;
AsciiPrint ("%a\r", Blank);
PxeBcDisplayBootItem (MenuArray[Select]->DescStr, MenuArray[Select]->DescLen);
gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);
LastSelect = Select;
while (gST->ConIn->ReadKeyStroke (gST->ConIn, &InputKey) == EFI_NOT_READY) {
gBS->Stall (10 * TICKS_PER_MS);
}
if (InputKey.ScanCode == 0) {
switch (InputKey.UnicodeChar) {
case CTRL ('c'):
InputKey.ScanCode = SCAN_ESC;
break;
case CTRL ('j'): /* linefeed */
case CTRL ('m'): /* return */
Finish = TRUE;
break;
case CTRL ('i'): /* tab */
case ' ':
case 'd':
case 'D':
InputKey.ScanCode = SCAN_DOWN;
break;
case CTRL ('h'): /* backspace */
case 'u':
case 'U':
InputKey.ScanCode = SCAN_UP;
break;
default:
InputKey.ScanCode = 0;
}
}
switch (InputKey.ScanCode) {
case SCAN_LEFT:
case SCAN_UP:
if (Select != 0) {
Select--;
}
break;
case SCAN_DOWN:
case SCAN_RIGHT:
if (++Select == MenuNum) {
Select--;
}
break;
case SCAN_PAGE_UP:
case SCAN_HOME:
Select = 0;
break;
case SCAN_PAGE_DOWN:
case SCAN_END:
Select = (UINT16)(MenuNum - 1);
break;
case SCAN_ESC:
return EFI_ABORTED;
}
//
// Unhighlight the last selected row.
//
gST->ConOut->SetAttribute (gST->ConOut, EFI_TEXT_ATTR (EFI_LIGHTGRAY, EFI_BLACK));
gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + LastSelect);
ASSERT (LastSelect < PXEBC_MENU_MAX_NUM);
ASSERT (MenuArray[LastSelect] != NULL);
Blank[MenuArray[LastSelect]->DescLen] = 0;
AsciiPrint ("%a\r", Blank);
PxeBcDisplayBootItem (MenuArray[LastSelect]->DescStr, MenuArray[LastSelect]->DescLen);
gST->ConOut->SetCursorPosition (gST->ConOut, 0, TopRow + MenuNum);
} while (!Finish);
//
// Swap the byte order.
//
ASSERT (Select < PXEBC_MENU_MAX_NUM);
ASSERT (MenuArray[Select] != NULL);
CopyMem (Type, &MenuArray[Select]->Type, sizeof (UINT16));
*Type = NTOHS (*Type);
return EFI_SUCCESS;
}
/**
Parse out the boot information from the last Dhcp4 reply packet.
@param[in, out] Private Pointer to PxeBc private data.
@param[out] BufferSize Size of the boot file to be downloaded.
@retval EFI_SUCCESS Successfully parsed out all the boot information.
@retval Others Failed to parse out the boot information.
**/
EFI_STATUS
PxeBcDhcp4BootInfo (
IN OUT PXEBC_PRIVATE_DATA *Private,
OUT UINT64 *BufferSize
)
{
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
EFI_PXE_BASE_CODE_MODE *Mode;
EFI_STATUS Status;
PXEBC_DHCP4_PACKET_CACHE *Cache4;
UINT16 Value;
PXEBC_VENDOR_OPTION *VendorOpt;
PXEBC_BOOT_SVR_ENTRY *Entry;
PxeBc = &Private->PxeBc;
Mode = PxeBc->Mode;
Status = EFI_SUCCESS;
*BufferSize = 0;
//
// Get the last received Dhcp4 reply packet.
//
if (Mode->PxeReplyReceived) {
Cache4 = &Private->PxeReply.Dhcp4;
} else if (Mode->ProxyOfferReceived) {
Cache4 = &Private->ProxyOffer.Dhcp4;
} else {
Cache4 = &Private->DhcpAck.Dhcp4;
}
if (Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
//
// This should never happen in a correctly configured DHCP / PXE
// environment. One misconfiguration that can cause it is two DHCP servers
// mistakenly running on the same network segment at the same time, and
// racing each other in answering DHCP requests. Thus, the DHCP packets
// that the edk2 PXE client considers "belonging together" may actually be
// entirely independent, coming from two (competing) DHCP servers.
//
// Try to deal with this gracefully. Note that this check is not
// comprehensive, as we don't try to identify all such errors.
//
return EFI_PROTOCOL_ERROR;
}
//
// Parse the boot server address.
// If prompt/discover is disabled, get the first boot server from the boot servers list.
// Otherwise, parse the boot server Ipv4 address from next server address field in DHCP header.
// If all these fields are not available, use option 54 instead.
//
VendorOpt = &Cache4->VendorOpt;
if (IS_DISABLE_PROMPT_MENU (VendorOpt->DiscoverCtrl) && IS_VALID_BOOT_SERVERS (VendorOpt->BitMap)) {
Entry = VendorOpt->BootSvr;
if ((VendorOpt->BootSvrLen >= sizeof (PXEBC_BOOT_SVR_ENTRY)) && (Entry->IpCnt > 0)) {
CopyMem (
&Private->ServerIp,
&Entry->IpAddr[0],
sizeof (EFI_IPv4_ADDRESS)
);
}
}
if (Private->ServerIp.Addr[0] == 0) {
//
// ServerIp.Addr[0] equals zero means we failed to get IP address from boot server list.
// Try to use next server address field.
//
CopyMem (
&Private->ServerIp,
&Cache4->Packet.Offer.Dhcp4.Header.ServerAddr,
sizeof (EFI_IPv4_ADDRESS)
);
}
if (Private->ServerIp.Addr[0] == 0) {
//
// Still failed , use the IP address from option 54.
//
CopyMem (
&Private->ServerIp,
Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,
sizeof (EFI_IPv4_ADDRESS)
);
}
//
// Parse the boot file name by option.
//
Private->BootFileName = Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE]->Data;
if (Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN] != NULL) {
//
// Parse the boot file size by option.
//
CopyMem (&Value, Cache4->OptList[PXEBC_DHCP4_TAG_INDEX_BOOTFILE_LEN]->Data, sizeof (Value));
Value = NTOHS (Value);
//
// The field of boot file size is 512 bytes in unit.
//
*BufferSize = 512 * Value;
} else {
//
// Get the bootfile size by tftp command if no option available.
//
Status = PxeBc->Mtftp (
PxeBc,
EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
NULL,
FALSE,
BufferSize,
&Private->BlockSize,
&Private->ServerIp,
Private->BootFileName,
NULL,
FALSE
);
}
//
// Save the value of boot file size.
//
Private->BootFileSize = (UINTN)*BufferSize;
//
// Display all the information: boot server address, boot file name and boot file size.
//
AsciiPrint ("\n Server IP address is ");
PxeBcShowIp4Addr (&Private->ServerIp.v4);
AsciiPrint ("\n NBP filename is %a", Private->BootFileName);
AsciiPrint ("\n NBP filesize is %d Bytes", Private->BootFileSize);
return Status;
}
/**
Parse out the boot information from the last Dhcp6 reply packet.
@param[in, out] Private Pointer to PxeBc private data.
@param[out] BufferSize Size of the boot file to be downloaded.
@retval EFI_SUCCESS Successfully parsed out all the boot information.
@retval EFI_BUFFER_TOO_SMALL
@retval Others Failed to parse out the boot information.
**/
EFI_STATUS
PxeBcDhcp6BootInfo (
IN OUT PXEBC_PRIVATE_DATA *Private,
OUT UINT64 *BufferSize
)
{
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
EFI_PXE_BASE_CODE_MODE *Mode;
EFI_STATUS Status;
PXEBC_DHCP6_PACKET_CACHE *Cache6;
UINT16 Value;
PxeBc = &Private->PxeBc;
Mode = PxeBc->Mode;
Status = EFI_SUCCESS;
*BufferSize = 0;
//
// Get the last received Dhcp6 reply packet.
//
if (Mode->PxeReplyReceived) {
Cache6 = &Private->PxeReply.Dhcp6;
} else if (Mode->ProxyOfferReceived) {
Cache6 = &Private->ProxyOffer.Dhcp6;
} else {
Cache6 = &Private->DhcpAck.Dhcp6;
}
if (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL] == NULL) {
//
// This should never happen in a correctly configured DHCP / PXE
// environment. One misconfiguration that can cause it is two DHCP servers
// mistakenly running on the same network segment at the same time, and
// racing each other in answering DHCP requests. Thus, the DHCP packets
// that the edk2 PXE client considers "belonging together" may actually be
// entirely independent, coming from two (competing) DHCP servers.
//
// Try to deal with this gracefully. Note that this check is not
// comprehensive, as we don't try to identify all such errors.
//
return EFI_PROTOCOL_ERROR;
}
//
// Set the station address to IP layer.
//
Status = PxeBcSetIp6Address (Private);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Parse (m)tftp server ip address and bootfile name.
//
Status = PxeBcExtractBootFileUrl (
Private,
&Private->BootFileName,
&Private->ServerIp.v6,
(CHAR8 *)(Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->Data),
NTOHS (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_URL]->OpLen)
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Parse the value of boot file size.
//
if (Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM] != NULL) {
//
// Parse it out if have the boot file parameter option.
//
Status = PxeBcExtractBootFileParam ((CHAR8 *)Cache6->OptList[PXEBC_DHCP6_IDX_BOOT_FILE_PARAM]->Data, &Value);
if (EFI_ERROR (Status)) {
return Status;
}
//
// The field of boot file size is 512 bytes in unit.
//
*BufferSize = 512 * Value;
} else {
//
// Send get file size command by tftp if option unavailable.
//
Status = PxeBc->Mtftp (
PxeBc,
EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE,
NULL,
FALSE,
BufferSize,
&Private->BlockSize,
&Private->ServerIp,
Private->BootFileName,
NULL,
FALSE
);
}
//
// Save the value of boot file size.
//
Private->BootFileSize = (UINTN)*BufferSize;
//
// Display all the information: boot server address, boot file name and boot file size.
//
AsciiPrint ("\n Server IP address is ");
PxeBcShowIp6Addr (&Private->ServerIp.v6);
AsciiPrint ("\n NBP filename is %a", Private->BootFileName);
AsciiPrint ("\n NBP filesize is %d Bytes", Private->BootFileSize);
return Status;
}
/**
Extract the discover information and boot server entry from the
cached packets if unspecified.
@param[in] Private Pointer to PxeBc private data.
@param[in] Type The type of bootstrap to perform.
@param[in, out] DiscoverInfo Pointer to EFI_PXE_BASE_CODE_DISCOVER_INFO.
@param[out] BootEntry Pointer to PXEBC_BOOT_SVR_ENTRY.
@param[out] SrvList Pointer to EFI_PXE_BASE_CODE_SRVLIST.
@retval EFI_SUCCESS Successfully extracted the information.
@retval EFI_DEVICE_ERROR Failed to extract the information.
**/
EFI_STATUS
PxeBcExtractDiscoverInfo (
IN PXEBC_PRIVATE_DATA *Private,
IN UINT16 Type,
IN OUT EFI_PXE_BASE_CODE_DISCOVER_INFO **DiscoverInfo,
OUT PXEBC_BOOT_SVR_ENTRY **BootEntry,
OUT EFI_PXE_BASE_CODE_SRVLIST **SrvList
)
{
EFI_PXE_BASE_CODE_MODE *Mode;
PXEBC_DHCP4_PACKET_CACHE *Cache4;
PXEBC_VENDOR_OPTION *VendorOpt;
PXEBC_BOOT_SVR_ENTRY *Entry;
BOOLEAN IsFound;
EFI_PXE_BASE_CODE_DISCOVER_INFO *Info;
UINT16 Index;
Mode = Private->PxeBc.Mode;
Info = *DiscoverInfo;
if (Mode->UsingIpv6) {
Info->IpCnt = 1;
Info->UseUCast = TRUE;
Info->SrvList[0].Type = Type;
Info->SrvList[0].AcceptAnyResponse = FALSE;
//
// There is no vendor options specified in DHCPv6, so take BootFileUrl in the last cached packet.
//
CopyMem (&Info->SrvList[0].IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS));
*SrvList = Info->SrvList;
} else {
Entry = NULL;
IsFound = FALSE;
Cache4 = (Mode->ProxyOfferReceived) ? &Private->ProxyOffer.Dhcp4 : &Private->DhcpAck.Dhcp4;
VendorOpt = &Cache4->VendorOpt;
if (!Mode->DhcpAckReceived || !IS_VALID_DISCOVER_VENDOR_OPTION (VendorOpt->BitMap)) {
//
// Address is not acquired or no discovery options.
//
return EFI_INVALID_PARAMETER;
}
//
// Parse the boot server entry from the vendor option in the last cached packet.
//
Info->UseMCast = (BOOLEAN) !IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl);
Info->UseBCast = (BOOLEAN) !IS_DISABLE_BCAST_DISCOVER (VendorOpt->DiscoverCtrl);
Info->MustUseList = (BOOLEAN)IS_ENABLE_USE_SERVER_LIST (VendorOpt->DiscoverCtrl);
Info->UseUCast = (BOOLEAN)IS_VALID_BOOT_SERVERS (VendorOpt->BitMap);
if (Info->UseMCast) {
//
// Get the multicast discover ip address from vendor option if has.
//
CopyMem (&Info->ServerMCastIp.v4, &VendorOpt->DiscoverMcastIp, sizeof (EFI_IPv4_ADDRESS));
}
Info->IpCnt = 0;
if (Info->UseUCast) {
Entry = VendorOpt->BootSvr;
while (((UINT8)(Entry - VendorOpt->BootSvr)) < VendorOpt->BootSvrLen) {
if (Entry->Type == HTONS (Type)) {
IsFound = TRUE;
break;
}
Entry = GET_NEXT_BOOT_SVR_ENTRY (Entry);
}
if (!IsFound) {
return EFI_DEVICE_ERROR;
}
Info->IpCnt = Entry->IpCnt;
if (Info->IpCnt >= 1) {
*DiscoverInfo = AllocatePool (sizeof (*Info) + (Info->IpCnt - 1) * sizeof (**SrvList));
if (*DiscoverInfo == NULL) {
return EFI_OUT_OF_RESOURCES;
}
CopyMem (*DiscoverInfo, Info, sizeof (*Info));
Info = *DiscoverInfo;
}
for (Index = 0; Index < Info->IpCnt; Index++) {
CopyMem (&Info->SrvList[Index].IpAddr, &Entry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));
Info->SrvList[Index].AcceptAnyResponse = !Info->MustUseList;
Info->SrvList[Index].Type = NTOHS (Entry->Type);
}
}
*BootEntry = Entry;
*SrvList = Info->SrvList;
}
return EFI_SUCCESS;
}
/**
Build the discover packet and send out for boot server.
@param[in] Private Pointer to PxeBc private data.
@param[in] Type PxeBc option boot item type.
@param[in] Layer Pointer to option boot item layer.
@param[in] UseBis Use BIS or not.
@param[in] DestIp Pointer to the destination address.
@param[in] IpCount The count of the server address.
@param[in] SrvList Pointer to the server address list.
@retval EFI_SUCCESS Successfully discovered boot file.
@retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
@retval EFI_NOT_FOUND Can't get the PXE reply packet.
@retval Others Failed to discover boot file.
**/
EFI_STATUS
PxeBcDiscoverBootServer (
IN PXEBC_PRIVATE_DATA *Private,
IN UINT16 Type,
IN UINT16 *Layer,
IN BOOLEAN UseBis,
IN EFI_IP_ADDRESS *DestIp,
IN UINT16 IpCount,
IN EFI_PXE_BASE_CODE_SRVLIST *SrvList
)
{
if (Private->PxeBc.Mode->UsingIpv6) {
return PxeBcDhcp6Discover (
Private,
Type,
Layer,
UseBis,
DestIp
);
} else {
return PxeBcDhcp4Discover (
Private,
Type,
Layer,
UseBis,
DestIp,
IpCount,
SrvList
);
}
}
/**
Discover all the boot information for boot file.
@param[in, out] Private Pointer to PxeBc private data.
@param[out] BufferSize Size of the boot file to be downloaded.
@retval EFI_SUCCESS Successfully obtained all the boot information .
@retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
@retval EFI_ABORTED User cancel current operation.
@retval Others Failed to parse out the boot information.
**/
EFI_STATUS
PxeBcDiscoverBootFile (
IN OUT PXEBC_PRIVATE_DATA *Private,
OUT UINT64 *BufferSize
)
{
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
EFI_PXE_BASE_CODE_MODE *Mode;
EFI_STATUS Status;
UINT16 Type;
UINT16 Layer;
BOOLEAN UseBis;
PxeBc = &Private->PxeBc;
Mode = PxeBc->Mode;
Type = EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP;
Layer = EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL;
//
// Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
// other pxe boot information.
//
Status = PxeBc->Dhcp (PxeBc, TRUE);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Select a boot server from boot server list.
//
Status = PxeBcSelectBootPrompt (Private);
if (Status == EFI_SUCCESS) {
//
// Choose by user's input.
//
Status = PxeBcSelectBootMenu (Private, &Type, FALSE);
} else if (Status == EFI_TIMEOUT) {
//
// Choose by default item.
//
Status = PxeBcSelectBootMenu (Private, &Type, TRUE);
}
if (!EFI_ERROR (Status)) {
if (Type == EFI_PXE_BASE_CODE_BOOT_TYPE_BOOTSTRAP) {
//
// Local boot(PXE bootstrap server) need abort
//
return EFI_ABORTED;
}
//
// Start to discover the boot server to get (m)tftp server ip address, bootfile
// name and bootfile size.
//
UseBis = (BOOLEAN)(Mode->BisSupported && Mode->BisDetected);
Status = PxeBc->Discover (PxeBc, Type, &Layer, UseBis, NULL);
if (EFI_ERROR (Status)) {
return Status;
}
if (Mode->PxeReplyReceived && !Mode->ProxyOfferReceived) {
//
// Some network boot loader only search the packet in Mode.ProxyOffer to get its server
// IP address, so we need to store a copy of Mode.PxeReply packet into Mode.ProxyOffer.
//
if (Mode->UsingIpv6) {
CopyMem (
&Mode->ProxyOffer.Dhcpv6,
&Mode->PxeReply.Dhcpv6,
Private->PxeReply.Dhcp6.Packet.Ack.Length
);
} else {
CopyMem (
&Mode->ProxyOffer.Dhcpv4,
&Mode->PxeReply.Dhcpv4,
Private->PxeReply.Dhcp4.Packet.Ack.Length
);
}
Mode->ProxyOfferReceived = TRUE;
}
}
//
// Parse the boot information.
//
if (Mode->UsingIpv6) {
Status = PxeBcDhcp6BootInfo (Private, BufferSize);
} else {
Status = PxeBcDhcp4BootInfo (Private, BufferSize);
}
return Status;
}
/**
Install PxeBaseCodeCallbackProtocol if not installed before.
@param[in, out] Private Pointer to PxeBc private data.
@param[out] NewMakeCallback If TRUE, it is a new callback.
Otherwise, it is not new callback.
@retval EFI_SUCCESS PxeBaseCodeCallbackProtocol installed successfully.
@retval Others Failed to install PxeBaseCodeCallbackProtocol.
**/
EFI_STATUS
PxeBcInstallCallback (
IN OUT PXEBC_PRIVATE_DATA *Private,
OUT BOOLEAN *NewMakeCallback
)
{
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
EFI_STATUS Status;
//
// Check whether PxeBaseCodeCallbackProtocol already installed.
//
PxeBc = &Private->PxeBc;
Status = gBS->HandleProtocol (
Private->Mode.UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller,
&gEfiPxeBaseCodeCallbackProtocolGuid,
(VOID **)&Private->PxeBcCallback
);
if (Status == EFI_UNSUPPORTED) {
CopyMem (
&Private->LoadFileCallback,
&gPxeBcCallBackTemplate,
sizeof (EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL)
);
//
// Install a default callback if user didn't offer one.
//
Status = gBS->InstallProtocolInterface (
Private->Mode.UsingIpv6 ? &Private->Ip6Nic->Controller : &Private->Ip4Nic->Controller,
&gEfiPxeBaseCodeCallbackProtocolGuid,
EFI_NATIVE_INTERFACE,
&Private->LoadFileCallback
);
(*NewMakeCallback) = (BOOLEAN)(Status == EFI_SUCCESS);
Status = PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, NewMakeCallback);
if (EFI_ERROR (Status)) {
PxeBc->Stop (PxeBc);
return Status;
}
}
return EFI_SUCCESS;
}
/**
Uninstall PxeBaseCodeCallbackProtocol.
@param[in] Private Pointer to PxeBc private data.
@param[in] NewMakeCallback If TRUE, it is a new callback.
Otherwise, it is not new callback.
**/
VOID
PxeBcUninstallCallback (
IN PXEBC_PRIVATE_DATA *Private,
IN BOOLEAN NewMakeCallback
)
{
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
PxeBc = &Private->PxeBc;
if (NewMakeCallback) {
NewMakeCallback = FALSE;
PxeBc->SetParameters (PxeBc, NULL, NULL, NULL, NULL, &NewMakeCallback);
gBS->UninstallProtocolInterface (
Private->Mode.UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller,
&gEfiPxeBaseCodeCallbackProtocolGuid,
&Private->LoadFileCallback
);
}
}
/**
Download one of boot file in the list, and it's special for IPv6.
@param[in] Private Pointer to PxeBc private data.
@param[in, out] BufferSize Size of user buffer for input;
required buffer size for output.
@param[in] Buffer Pointer to user buffer.
@retval EFI_SUCCESS Read one of boot file in the list successfully.
@retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
@retval EFI_NOT_FOUND There is no proper boot file available.
@retval Others Failed to download boot file in the list.
**/
EFI_STATUS
PxeBcReadBootFileList (
IN PXEBC_PRIVATE_DATA *Private,
IN OUT UINT64 *BufferSize,
IN VOID *Buffer OPTIONAL
)
{
EFI_STATUS Status;
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
PxeBc = &Private->PxeBc;
//
// Try to download the boot file if everything is ready.
//
if (Buffer != NULL) {
Status = PxeBc->Mtftp (
PxeBc,
EFI_PXE_BASE_CODE_TFTP_READ_FILE,
Buffer,
FALSE,
BufferSize,
&Private->BlockSize,
&Private->ServerIp,
Private->BootFileName,
NULL,
FALSE
);
} else {
Status = EFI_BUFFER_TOO_SMALL;
}
return Status;
}
/**
Load boot file into user buffer.
@param[in] Private Pointer to PxeBc private data.
@param[in, out] BufferSize Size of user buffer for input;
required buffer size for output.
@param[in] Buffer Pointer to user buffer.
@retval EFI_SUCCESS Get all the boot information successfully.
@retval EFI_BUFFER_TOO_SMALL The buffer size is not enough for boot file.
@retval EFI_ABORTED User cancelled the current operation.
@retval Others Failed to parse out the boot information.
**/
EFI_STATUS
PxeBcLoadBootFile (
IN PXEBC_PRIVATE_DATA *Private,
IN OUT UINTN *BufferSize,
IN VOID *Buffer OPTIONAL
)
{
BOOLEAN NewMakeCallback;
UINT64 RequiredSize;
UINT64 CurrentSize;
EFI_STATUS Status;
EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
EFI_PXE_BASE_CODE_MODE *PxeBcMode;
NewMakeCallback = FALSE;
PxeBc = &Private->PxeBc;
PxeBcMode = &Private->Mode;
CurrentSize = *BufferSize;
RequiredSize = 0;
//
// Install pxebc callback protocol if hasn't been installed yet.
//
Status = PxeBcInstallCallback (Private, &NewMakeCallback);
if (EFI_ERROR (Status)) {
return Status;
}
if (Private->BootFileSize == 0) {
//
// Discover the boot information about the bootfile if hasn't.
//
Status = PxeBcDiscoverBootFile (Private, &RequiredSize);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
}
if (PXEBC_IS_SIZE_OVERFLOWED (RequiredSize)) {
//
// It's error if the required buffer size is beyond the system scope.
//
Status = EFI_DEVICE_ERROR;
goto ON_EXIT;
} else if (RequiredSize > 0) {
//
// Get the right buffer size of the bootfile required.
//
if ((CurrentSize < RequiredSize) || (Buffer == NULL)) {
//
// It's buffer too small if the size of user buffer is smaller than the required.
//
CurrentSize = RequiredSize;
Status = EFI_BUFFER_TOO_SMALL;
goto ON_EXIT;
}
CurrentSize = RequiredSize;
} else if ((RequiredSize == 0) && PxeBcMode->UsingIpv6) {
//
// Try to download another bootfile in list if failed to get the filesize of the last one.
// It's special for the case of IPv6.
//
Status = PxeBcReadBootFileList (Private, &CurrentSize, Buffer);
goto ON_EXIT;
}
} else if ((CurrentSize < Private->BootFileSize) || (Buffer == NULL)) {
//
// It's buffer too small if the size of user buffer is smaller than the required.
//
CurrentSize = Private->BootFileSize;
Status = EFI_BUFFER_TOO_SMALL;
goto ON_EXIT;
}
//
// Begin to download the bootfile if everything is ready.
//
AsciiPrint ("\n Downloading NBP file...\n");
if (PxeBcMode->UsingIpv6) {
Status = PxeBcReadBootFileList (
Private,
&CurrentSize,
Buffer
);
} else {
Status = PxeBc->Mtftp (
PxeBc,
EFI_PXE_BASE_CODE_TFTP_READ_FILE,
Buffer,
FALSE,
&CurrentSize,
&Private->BlockSize,
&Private->ServerIp,
Private->BootFileName,
NULL,
FALSE
);
}
ON_EXIT:
*BufferSize = (UINTN)CurrentSize;
PxeBcUninstallCallback (Private, NewMakeCallback);
if (Status == EFI_SUCCESS) {
AsciiPrint ("\n NBP file downloaded successfully.\n");
return EFI_SUCCESS;
} else if ((Status == EFI_BUFFER_TOO_SMALL) && (Buffer != NULL)) {
AsciiPrint ("\n PXE-E05: Buffer size is smaller than the requested file.\n");
} else if (Status == EFI_DEVICE_ERROR) {
AsciiPrint ("\n PXE-E07: Network device error.\n");
} else if (Status == EFI_OUT_OF_RESOURCES) {
AsciiPrint ("\n PXE-E09: Could not allocate I/O buffers.\n");
} else if (Status == EFI_NO_MEDIA) {
AsciiPrint ("\n PXE-E12: Could not detect network connection.\n");
} else if (Status == EFI_NO_RESPONSE) {
AsciiPrint ("\n PXE-E16: No valid offer received.\n");
} else if (Status == EFI_TIMEOUT) {
AsciiPrint ("\n PXE-E18: Server response timeout.\n");
} else if (Status == EFI_ABORTED) {
AsciiPrint ("\n PXE-E21: Remote boot cancelled.\n");
} else if (Status == EFI_ICMP_ERROR) {
AsciiPrint ("\n PXE-E22: Client received ICMP error from server.\n");
} else if (Status == EFI_TFTP_ERROR) {
AsciiPrint ("\n PXE-E23: Client received TFTP error from server.\n");
} else if (Status == EFI_NOT_FOUND) {
AsciiPrint ("\n PXE-E53: No boot filename received.\n");
} else if (Status != EFI_BUFFER_TOO_SMALL) {
AsciiPrint ("\n PXE-E99: Unexpected network error.\n");
}
REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
EFI_ERROR_CODE,
(EFI_STATUS_CODE_VALUE)(EFI_IO_BUS_IP_NETWORK | EFI_OEM_SPECIFIC | ((EFI_STATUS_CODE_VALUE)(Status & 0x1F))),
(VOID *)&(PxeBcMode->UsingIpv6),
sizeof (PxeBcMode->UsingIpv6)
);
return Status;
}
|