forked from geneC/syslinux
/
pxelinux.asm
2897 lines (2591 loc) · 62.4 KB
/
pxelinux.asm
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
; -*- fundamental -*- (asm-mode sucks)
; ****************************************************************************
;
; pxelinux.asm
;
; A program to boot Linux kernels off a TFTP server using the Intel PXE
; network booting API. It is based on the SYSLINUX boot loader for
; MS-DOS floppies.
;
; Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
; Boston MA 02111-1307, USA; either version 2 of the License, or
; (at your option) any later version; incorporated herein by reference.
;
; ****************************************************************************
%define IS_PXELINUX 1
%include "head.inc"
%include "pxe.inc"
; gPXE extensions support
%define GPXE 1
;
; Some semi-configurable constants... change on your own risk.
;
my_id equ pxelinux_id
FILENAME_MAX_LG2 equ 7 ; log2(Max filename size Including final null)
FILENAME_MAX equ (1 << FILENAME_MAX_LG2)
NULLFILE equ 0 ; Zero byte == null file name
NULLOFFSET equ 4 ; Position in which to look
REBOOT_TIME equ 5*60 ; If failure, time until full reset
%assign HIGHMEM_SLOP 128*1024 ; Avoid this much memory near the top
MAX_OPEN_LG2 equ 5 ; log2(Max number of open sockets)
MAX_OPEN equ (1 << MAX_OPEN_LG2)
PKTBUF_SIZE equ (65536/MAX_OPEN) ; Per-socket packet buffer size
TFTP_PORT equ htons(69) ; Default TFTP port
PKT_RETRY equ 6 ; Packet transmit retry count
PKT_TIMEOUT equ 12 ; Initial timeout, timer ticks @ 55 ms
; Desired TFTP block size
; For Ethernet MTU is normally 1500. Unfortunately there seems to
; be a fair number of networks with "substandard" MTUs which break.
; The code assumes TFTP_LARGEBLK <= 2K.
TFTP_MTU equ 1440
TFTP_LARGEBLK equ (TFTP_MTU-20-8-4) ; MTU - IP hdr - UDP hdr - TFTP hdr
; Standard TFTP block size
TFTP_BLOCKSIZE_LG2 equ 9 ; log2(bytes/block)
TFTP_BLOCKSIZE equ (1 << TFTP_BLOCKSIZE_LG2)
%assign USE_PXE_PROVIDED_STACK 1 ; Use stack provided by PXE?
SECTOR_SHIFT equ TFTP_BLOCKSIZE_LG2
SECTOR_SIZE equ TFTP_BLOCKSIZE
;
; This is what we need to do when idle
; *** This is disabled because some PXE stacks wait for unacceptably
; *** long if there are no packets receivable.
%define HAVE_IDLE 0 ; idle is not a noop
%if HAVE_IDLE
%macro RESET_IDLE 0
call reset_idle
%endmacro
%macro DO_IDLE 0
call check_for_arp
%endmacro
%else
%macro RESET_IDLE 0
; Nothing
%endmacro
%macro DO_IDLE 0
; Nothing
%endmacro
%endif
;
; TFTP operation codes
;
TFTP_RRQ equ htons(1) ; Read request
TFTP_WRQ equ htons(2) ; Write request
TFTP_DATA equ htons(3) ; Data packet
TFTP_ACK equ htons(4) ; ACK packet
TFTP_ERROR equ htons(5) ; ERROR packet
TFTP_OACK equ htons(6) ; OACK packet
;
; TFTP error codes
;
TFTP_EUNDEF equ htons(0) ; Unspecified error
TFTP_ENOTFOUND equ htons(1) ; File not found
TFTP_EACCESS equ htons(2) ; Access violation
TFTP_ENOSPACE equ htons(3) ; Disk full
TFTP_EBADOP equ htons(4) ; Invalid TFTP operation
TFTP_EBADID equ htons(5) ; Unknown transfer
TFTP_EEXISTS equ htons(6) ; File exists
TFTP_ENOUSER equ htons(7) ; No such user
TFTP_EOPTNEG equ htons(8) ; Option negotiation failure
;
; The following structure is used for "virtual kernels"; i.e. LILO-style
; option labels. The options we permit here are `kernel' and `append
; Since there is no room in the bottom 64K for all of these, we
; stick them in high memory and copy them down before we need them.
;
struc vkernel
vk_vname: resb FILENAME_MAX ; Virtual name **MUST BE FIRST!**
vk_rname: resb FILENAME_MAX ; Real name
vk_ipappend: resb 1 ; "IPAPPEND" flag
vk_type: resb 1 ; Type of file
vk_appendlen: resw 1
alignb 4
vk_append: resb max_cmd_len+1 ; Command line
alignb 4
vk_end: equ $ ; Should be <= vk_size
endstruc
;
; Segment assignments in the bottom 640K
; 0000h - main code/data segment (and BIOS segment)
;
real_mode_seg equ 3000h
pktbuf_seg equ 2000h ; Packet buffers segments
xfer_buf_seg equ 1000h ; Bounce buffer for I/O to high mem
comboot_seg equ real_mode_seg ; COMBOOT image loading zone
;
; BOOTP/DHCP packet pattern
;
struc bootp_t
bootp:
.opcode resb 1 ; BOOTP/DHCP "opcode"
.hardware resb 1 ; ARP hardware type
.hardlen resb 1 ; Hardware address length
.gatehops resb 1 ; Used by forwarders
.ident resd 1 ; Transaction ID
.seconds resw 1 ; Seconds elapsed
.flags resw 1 ; Broadcast flags
.cip resd 1 ; Client IP
.yip resd 1 ; "Your" IP
.sip resd 1 ; Next server IP
.gip resd 1 ; Relay agent IP
.macaddr resb 16 ; Client MAC address
.sname resb 64 ; Server name (optional)
.bootfile resb 128 ; Boot file name
.option_magic resd 1 ; Vendor option magic cookie
.options resb 1260 ; Vendor options
endstruc
BOOTP_OPTION_MAGIC equ htonl(0x63825363) ; See RFC 2132
;
; TFTP connection data structure. Each one of these corresponds to a local
; UDP port. The size of this structure must be a power of 2.
; HBO = host byte order; NBO = network byte order
; (*) = written by options negotiation code, must be dword sized
;
; For a gPXE connection, we set the local port number to -1 and the
; remote port number contains the gPXE file handle.
;
struc open_file_t
tftp_localport resw 1 ; Local port number (0 = not in use)
tftp_remoteport resw 1 ; Remote port number
tftp_remoteip resd 1 ; Remote IP address
tftp_filepos resd 1 ; Bytes downloaded (including buffer)
tftp_filesize resd 1 ; Total file size(*)
tftp_blksize resd 1 ; Block size for this connection(*)
tftp_bytesleft resw 1 ; Unclaimed data bytes
tftp_lastpkt resw 1 ; Sequence number of last packet (NBO)
tftp_dataptr resw 1 ; Pointer to available data
tftp_goteof resb 1 ; 1 if the EOF packet received
resb 3 ; Currently unusued
; At end since it should not be zeroed on socked close
tftp_pktbuf resw 1 ; Packet buffer offset
endstruc
%ifndef DEPEND
%if (open_file_t_size & (open_file_t_size-1))
%error "open_file_t is not a power of 2"
%endif
%endif
; ---------------------------------------------------------------------------
; BEGIN CODE
; ---------------------------------------------------------------------------
;
; Memory below this point is reserved for the BIOS and the MBR
;
section .earlybss
trackbufsize equ 8192
trackbuf resb trackbufsize ; Track buffer goes here
; ends at 2800h
alignb open_file_t_size
Files resb MAX_OPEN*open_file_t_size
alignb FILENAME_MAX
BootFile resb 256 ; Boot file from DHCP packet
PathPrefix resb 256 ; Path prefix derived from boot file
DotQuadBuf resb 16 ; Buffer for dotted-quad IP address
IPOption resb 80 ; ip= option buffer
InitStack resd 1 ; Pointer to reset stack (SS:SP)
PXEStack resd 1 ; Saved stack during PXE call
section .bss
alignb 4
RebootTime resd 1 ; Reboot timeout, if set by option
StrucPtr resd 1 ; Pointer to PXENV+ or !PXE structure
APIVer resw 1 ; PXE API version found
IPOptionLen resw 1 ; Length of IPOption
IdleTimer resw 1 ; Time to check for ARP?
LocalBootType resw 1 ; Local boot return code
PktTimeout resw 1 ; Timeout for current packet
RealBaseMem resw 1 ; Amount of DOS memory after freeing
OverLoad resb 1 ; Set if DHCP packet uses "overloading"
DHCPMagic resb 1 ; PXELINUX magic flags
; The relative position of these fields matter!
MAC_MAX equ 32 ; Handle hardware addresses this long
MACLen resb 1 ; MAC address len
MACType resb 1 ; MAC address type
MAC resb MAC_MAX+1 ; Actual MAC address
BOOTIFStr resb 7 ; Space for "BOOTIF="
MACStr resb 3*(MAC_MAX+1) ; MAC address as a string
; The relative position of these fields matter!
UUIDType resb 1 ; Type byte from DHCP option
UUID resb 16 ; UUID, from the PXE stack
UUIDNull resb 1 ; dhcp_copyoption zero-terminates
;
; PXE packets which don't need static initialization
;
alignb 4
pxe_unload_stack_pkt:
.status: resw 1 ; Status
.reserved: resw 10 ; Reserved
pxe_unload_stack_pkt_len equ $-pxe_unload_stack_pkt
alignb 16
; BOOTP/DHCP packet buffer
section .bss2
alignb 16
packet_buf resb 2048 ; Transfer packet
packet_buf_size equ $-packet_buf
section .text
;
; PXELINUX needs more BSS than the other derivatives;
; therefore we relocate it from 7C00h on startup.
;
StackBuf equ $ ; Base of stack if we use our own
;
; Primary entry point.
;
bootsec equ $
_start:
pushfd ; Paranoia... in case of return to PXE
pushad ; ... save as much state as possible
push ds
push es
push fs
push gs
xor ax,ax
mov ds,ax
mov es,ax
jmp 0:_start1 ; Canonicalize address
_start1:
mov bp,sp
les bx,[bp+48] ; ES:BX -> !PXE or PXENV+ structure
; That is all pushed onto the PXE stack. Save the pointer
; to it and switch to an internal stack.
mov [InitStack],sp
mov [InitStack+2],ss
%if USE_PXE_PROVIDED_STACK
; Apparently some platforms go bonkers if we
; set up our own stack...
mov [BaseStack],sp
mov [BaseStack+4],ss
%endif
cli ; Paranoia
lss esp,[BaseStack]
sti ; Stack set up and ready
cld ; Copy upwards
;
; Initialize screen (if we're using one)
;
push es ; Save ES -> PXE entry structure
push ds
pop es ; ES <- DS
%include "init.inc"
pop es ; Restore ES -> PXE entry structure
;
; Tell the user we got this far
;
mov si,syslinux_banner
call writestr
mov si,copyright_str
call writestr
;
; Assume API version 2.1, in case we find the !PXE structure without
; finding the PXENV+ structure. This should really look at the Base
; Code ROM ID structure in have_pxe, but this is adequate for now --
; if we have !PXE, we have to be 2.1 or higher, and we don't care
; about higher versions than that.
;
mov word [APIVer],0201h
;
; Now we need to find the !PXE structure. It's *supposed* to be pointed
; to by SS:[SP+4], but support INT 1Ah, AX=5650h method as well.
; FIX: ES:BX should point to the PXENV+ structure on entry as well.
; We should make that the second test, and not trash ES:BX...
;
cmp dword [es:bx], '!PXE'
je have_pxe
; Uh-oh, not there... try plan B
mov ax, 5650h
%if USE_PXE_PROVIDED_STACK == 0
lss sp,[InitStack]
%endif
int 1Ah ; May trash regs
%if USE_PXE_PROVIDED_STACK == 0
lss esp,[BaseStack]
%endif
jc no_pxe
cmp ax,564Eh
jne no_pxe
; Okay, that gave us the PXENV+ structure, find !PXE
; structure from that (if available)
cmp dword [es:bx], 'PXEN'
jne no_pxe
cmp word [es:bx+4], 'V+'
je have_pxenv
; Nothing there either. Last-ditch: scan memory
call memory_scan_for_pxe_struct ; !PXE scan
jnc have_pxe
call memory_scan_for_pxenv_struct ; PXENV+ scan
jnc have_pxenv
no_pxe: mov si,err_nopxe
call writestr
jmp kaboom
have_pxenv:
mov [StrucPtr],bx
mov [StrucPtr+2],es
mov si,found_pxenv
call writestr
mov si,apiver_str
call writestr
mov ax,[es:bx+6]
mov [APIVer],ax
call writehex4
call crlf
cmp ax,0201h ; API version 2.1 or higher
jb old_api
mov si,bx
mov ax,es
les bx,[es:bx+28h] ; !PXE structure pointer
cmp dword [es:bx],'!PXE'
je have_pxe
; Nope, !PXE structure missing despite API 2.1+, or at least
; the pointer is missing. Do a last-ditch attempt to find it.
call memory_scan_for_pxe_struct
jnc have_pxe
; Otherwise, no dice, use PXENV+ structure
mov bx,si
mov es,ax
old_api: ; Need to use a PXENV+ structure
mov si,using_pxenv_msg
call writestr
mov eax,[es:bx+0Ah] ; PXE RM API
mov [PXEEntry],eax
mov si,undi_data_msg
call writestr
mov ax,[es:bx+20h]
call writehex4
call crlf
mov si,undi_data_len_msg
call writestr
mov ax,[es:bx+22h]
call writehex4
call crlf
mov si,undi_code_msg
call writestr
mov ax,[es:bx+24h]
call writehex4
call crlf
mov si,undi_code_len_msg
call writestr
mov ax,[es:bx+26h]
call writehex4
call crlf
; Compute base memory size from PXENV+ structure
xor esi,esi
movzx eax,word [es:bx+20h] ; UNDI data seg
cmp ax,[es:bx+24h] ; UNDI code seg
ja .use_data
mov ax,[es:bx+24h]
mov si,[es:bx+26h]
jmp short .combine
.use_data:
mov si,[es:bx+22h]
.combine:
shl eax,4
add eax,esi
shr eax,10 ; Convert to kilobytes
mov [RealBaseMem],ax
mov si,pxenventry_msg
call writestr
mov ax,[PXEEntry+2]
call writehex4
mov al,':'
call writechr
mov ax,[PXEEntry]
call writehex4
call crlf
jmp have_entrypoint
have_pxe:
mov [StrucPtr],bx
mov [StrucPtr+2],es
mov eax,[es:bx+10h]
mov [PXEEntry],eax
mov si,undi_data_msg
call writestr
mov eax,[es:bx+2Ah]
call writehex8
call crlf
mov si,undi_data_len_msg
call writestr
mov ax,[es:bx+2Eh]
call writehex4
call crlf
mov si,undi_code_msg
call writestr
mov ax,[es:bx+32h]
call writehex8
call crlf
mov si,undi_code_len_msg
call writestr
mov ax,[es:bx+36h]
call writehex4
call crlf
; Compute base memory size from !PXE structure
xor esi,esi
mov eax,[es:bx+2Ah]
cmp eax,[es:bx+32h]
ja .use_data
mov eax,[es:bx+32h]
mov si,[es:bx+36h]
jmp short .combine
.use_data:
mov si,[es:bx+2Eh]
.combine:
add eax,esi
shr eax,10
mov [RealBaseMem],ax
mov si,pxeentry_msg
call writestr
mov ax,[PXEEntry+2]
call writehex4
mov al,':'
call writechr
mov ax,[PXEEntry]
call writehex4
call crlf
have_entrypoint:
push cs
pop es ; Restore CS == DS == ES
;
; Network-specific initialization
;
xor ax,ax
mov [LocalDomain],al ; No LocalDomain received
;
; The DHCP client identifiers are best gotten from the DHCPREQUEST
; packet (query info 1).
;
query_bootp_1:
mov dl,1
call pxe_get_cached_info
call parse_dhcp
; We don't use flags from the request packet, so
; this is a good time to initialize DHCPMagic...
; Initialize it to 1 meaning we will accept options found;
; in earlier versions of PXELINUX bit 0 was used to indicate
; we have found option 208 with the appropriate magic number;
; we no longer require that, but MAY want to re-introduce
; it in the future for vendor encapsulated options.
mov byte [DHCPMagic],1
;
; Now attempt to get the BOOTP/DHCP packet that brought us life (and an IP
; address). This lives in the DHCPACK packet (query info 2).
;
query_bootp_2:
mov dl,2
call pxe_get_cached_info
call parse_dhcp ; Parse DHCP packet
;
; Save away MAC address (assume this is in query info 2. If this
; turns out to be problematic it might be better getting it from
; the query info 1 packet.)
;
.save_mac:
movzx cx,byte [trackbuf+bootp.hardlen]
cmp cx,16
jna .mac_ok
xor cx,cx ; Bad hardware address length
.mac_ok:
mov [MACLen],cl
mov al,[trackbuf+bootp.hardware]
mov [MACType],al
mov si,trackbuf+bootp.macaddr
mov di,MAC
rep movsb
; Enable this if we really need to zero-pad this field...
; mov cx,MAC+MAC_MAX+1
; sub cx,di
; xor ax,ax
; rep stosb
;
; Now, get the boot file and other info. This lives in the CACHED_REPLY
; packet (query info 3).
;
mov dl,3
call pxe_get_cached_info
call parse_dhcp ; Parse DHCP packet
;
; Generate the bootif string, and the hardware-based config string.
;
make_bootif_string:
mov si,bootif_str
mov di,BOOTIFStr
mov cx,bootif_str_len
rep movsb
movzx cx,byte [MACLen]
mov si,MACType
inc cx
.hexify_mac:
push cx
mov cl,1 ; CH == 0 already
call lchexbytes
mov al,'-'
stosb
pop cx
loop .hexify_mac
mov [di-1],cl ; Null-terminate and strip final dash
;
; Generate ip= option
;
call genipopt
;
; Print IP address
;
mov eax,[MyIP]
mov di,DotQuadBuf
push di
call gendotquad ; This takes network byte order input
xchg ah,al ; Convert to host byte order
ror eax,16 ; (BSWAP doesn't work on 386)
xchg ah,al
mov si,myipaddr_msg
call writestr
call writehex8
mov al,' '
call writechr
pop si ; DotQuadBuf
call writestr
call crlf
mov si,IPOption
call writestr
call crlf
;
; Check to see if we got any PXELINUX-specific DHCP options; in particular,
; if we didn't get the magic enable, do not recognize any other options.
;
check_dhcp_magic:
test byte [DHCPMagic], 1 ; If we didn't get the magic enable...
jnz .got_magic
mov byte [DHCPMagic], 0 ; If not, kill all other options
.got_magic:
;
; Initialize UDP stack
;
udp_init:
mov eax,[MyIP]
mov [pxe_udp_open_pkt.sip],eax
mov di,pxe_udp_open_pkt
mov bx,PXENV_UDP_OPEN
call pxenv
jc .failed
cmp word [pxe_udp_open_pkt.status], byte 0
je .success
.failed: mov si,err_udpinit
call writestr
jmp kaboom
.success:
;
; Common initialization code
;
%include "cpuinit.inc"
;
; Now we're all set to start with our *real* business. First load the
; configuration file (if any) and parse it.
;
; In previous versions I avoided using 32-bit registers because of a
; rumour some BIOSes clobbered the upper half of 32-bit registers at
; random. I figure, though, that if there are any of those still left
; they probably won't be trying to install Linux on them...
;
; The code is still ripe with 16-bitisms, though. Not worth the hassle
; to take'm out. In fact, we may want to put them back if we're going
; to boot ELKS at some point.
;
;
; Store standard filename prefix
;
prefix: test byte [DHCPMagic], 04h ; Did we get a path prefix option
jnz .got_prefix
mov si,BootFile
mov di,PathPrefix
cld
call strcpy
mov cx,di
sub cx,PathPrefix+1
std
lea si,[di-2] ; Skip final null!
.find_alnum: lodsb
or al,20h
cmp al,'.' ; Count . or - as alphanum
je .alnum
cmp al,'-'
je .alnum
cmp al,'0'
jb .notalnum
cmp al,'9'
jbe .alnum
cmp al,'a'
jb .notalnum
cmp al,'z'
ja .notalnum
.alnum: loop .find_alnum
dec si
.notalnum: mov byte [si+2],0 ; Zero-terminate after delimiter
cld
.got_prefix:
mov si,tftpprefix_msg
call writestr
mov si,PathPrefix
call writestr
call crlf
;
; Load configuration file
;
find_config:
;
; Begin looking for configuration file
;
config_scan:
test byte [DHCPMagic], 02h
jz .no_option
; We got a DHCP option, try it first
call .try
jnz .success
.no_option:
mov di,ConfigName
mov si,cfgprefix
mov cx,cfgprefix_len
rep movsb
; Have to guess config file name...
; Try loading by UUID.
cmp byte [HaveUUID],0
je .no_uuid
push di
mov bx,uuid_dashes
mov si,UUID
.gen_uuid:
movzx cx,byte [bx]
jcxz .done_uuid
inc bx
call lchexbytes
mov al,'-'
stosb
jmp .gen_uuid
.done_uuid:
mov [di-1],cl ; Remove last dash and zero-terminate
pop di
call .try
jnz .success
.no_uuid:
; Try loading by MAC address
push di
mov si,MACStr
call strcpy
pop di
call .try
jnz .success
; Nope, try hexadecimal IP prefixes...
.scan_ip:
mov cx,4
mov si,MyIP
call uchexbytes ; Convert to hex string
mov cx,8 ; Up to 8 attempts
.tryagain:
mov byte [di],0 ; Zero-terminate string
call .try
jnz .success
dec di ; Drop one character
loop .tryagain
; Final attempt: "default" string
mov si,default_str ; "default" string
call strcpy
call .try
jnz .success
mov si,err_noconfig
call writestr
jmp kaboom
.try:
pusha
mov si,trying_msg
call writestr
mov di,ConfigName
mov si,di
call writestr
call crlf
mov si,di
mov di,KernelName ; Borrow this buffer for mangled name
call mangle_name
call open
popa
ret
.success:
;
; Linux kernel loading code is common. However, we need to define
; a couple of helper macros...
;
; Handle "ipappend" option
%define HAVE_SPECIAL_APPEND
%macro SPECIAL_APPEND 0
test byte [IPAppend],01h ; ip=
jz .noipappend1
mov si,IPOption
mov cx,[IPOptionLen]
rep movsb
mov al,' '
stosb
.noipappend1:
test byte [IPAppend],02h
jz .noipappend2
mov si,BOOTIFStr
call strcpy
mov byte [es:di-1],' ' ; Replace null with space
.noipappend2:
%endmacro
; Unload PXE stack
%define HAVE_UNLOAD_PREP
%macro UNLOAD_PREP 0
call unload_pxe
%endmacro
;
; Now we have the config file open. Parse the config file and
; run the user interface.
;
%include "ui.inc"
;
; Boot to the local disk by returning the appropriate PXE magic.
; AX contains the appropriate return code.
;
%if HAS_LOCALBOOT
local_boot:
push cs
pop ds
mov [LocalBootType],ax
call vgaclearmode
mov si,localboot_msg
call writestr
; Restore the environment we were called with
lss sp,[InitStack]
pop gs
pop fs
pop es
pop ds
popad
mov ax,[cs:LocalBootType]
popfd
retf ; Return to PXE
%endif
;
; kaboom: write a message and bail out. Wait for quite a while,
; or a user keypress, then do a hard reboot.
;
kaboom:
RESET_STACK_AND_SEGS AX
.patch: mov si,bailmsg
call writestr ; Returns with AL = 0
.drain: call pollchar
jz .drained
call getchar
jmp short .drain
.drained:
mov edi,[RebootTime]
mov al,[DHCPMagic]
and al,09h ; Magic+Timeout
cmp al,09h
je .time_set
mov edi,REBOOT_TIME
.time_set:
mov cx,18
.wait1: push cx
mov ecx,edi
.wait2: mov dx,[BIOS_timer]
.wait3: call pollchar
jnz .keypress
cmp dx,[BIOS_timer]
je .wait3
loop .wait2,ecx
mov al,'.'
call writechr
pop cx
loop .wait1
.keypress:
call crlf
mov word [BIOS_magic],0 ; Cold reboot
jmp 0F000h:0FFF0h ; Reset vector address
;
; memory_scan_for_pxe_struct:
;
; If none of the standard methods find the !PXE structure, look for it
; by scanning memory.
;
; On exit, if found:
; CF = 0, ES:BX -> !PXE structure
; Otherwise CF = 1, all registers saved
;
memory_scan_for_pxe_struct:
push ds
pusha
mov ax,cs
mov ds,ax
mov si,trymempxe_msg
call writestr
mov ax,[BIOS_fbm] ; Starting segment
shl ax,(10-4) ; Kilobytes -> paragraphs
; mov ax,01000h ; Start to look here
dec ax ; To skip inc ax
.mismatch:
inc ax
cmp ax,0A000h ; End of memory
jae .not_found
call writehex4
mov si,fourbs_msg
call writestr
mov es,ax
mov edx,[es:0]
cmp edx,'!PXE'
jne .mismatch
movzx cx,byte [es:4] ; Length of structure
cmp cl,08h ; Minimum length
jb .mismatch
push ax
xor ax,ax
xor si,si
.checksum: es lodsb
add ah,al
loop .checksum
pop ax
jnz .mismatch ; Checksum must == 0
.found: mov bp,sp
xor bx,bx
mov [bp+8],bx ; Save BX into stack frame (will be == 0)
mov ax,es
call writehex4
call crlf
popa
pop ds
clc
ret
.not_found: mov si,notfound_msg
call writestr
popa
pop ds
stc
ret
;
; memory_scan_for_pxenv_struct:
;
; If none of the standard methods find the PXENV+ structure, look for it
; by scanning memory.
;
; On exit, if found:
; CF = 0, ES:BX -> PXENV+ structure
; Otherwise CF = 1, all registers saved
;
memory_scan_for_pxenv_struct:
pusha
mov si,trymempxenv_msg
call writestr
; mov ax,[BIOS_fbm] ; Starting segment
; shl ax,(10-4) ; Kilobytes -> paragraphs
mov ax,01000h ; Start to look here
dec ax ; To skip inc ax
.mismatch:
inc ax
cmp ax,0A000h ; End of memory
jae .not_found
mov es,ax
mov edx,[es:0]
cmp edx,'PXEN'
jne .mismatch
mov dx,[es:4]
cmp dx,'V+'
jne .mismatch
movzx cx,byte [es:8] ; Length of structure
cmp cl,26h ; Minimum length
jb .mismatch
xor ax,ax
xor si,si
.checksum: es lodsb
add ah,al
loop .checksum
and ah,ah
jnz .mismatch ; Checksum must == 0