forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxe.go
63 lines (55 loc) · 1.87 KB
/
pxe.go
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
// Copyright 2017-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package pxe implements the PXE config file parsing.
//
// See http://www.pix.net/software/pxeboot/archive/pxespec.pdf
package pxe
import (
"context"
"encoding/hex"
"fmt"
"net"
"net/url"
"path"
"strings"
"github.com/u-root/u-root/pkg/boot"
"github.com/u-root/u-root/pkg/boot/syslinux"
"github.com/u-root/u-root/pkg/curl"
)
// ParseConfig probes for config files based on the Mac and IP given
// and uses s to fetch files.
func ParseConfig(ctx context.Context, workingDir *url.URL, mac net.HardwareAddr, ip net.IP, s curl.Schemes) ([]boot.OSImage, error) {
rootDir := *workingDir
rootDir.Path = ""
for _, relname := range probeFiles(mac, ip) {
// "When booting, the initial working directory for PXELINUX
// will be the parent directory of pxelinux.0 unless overridden
// with DHCP option 210."
//
// https://wiki.syslinux.org/wiki/index.php?title=Config#Working_directory
imgs, err := syslinux.ParseConfigFile(ctx, s, path.Join("pxelinux.cfg", relname), &rootDir, workingDir.Path)
if curl.IsURLError(err) {
// We didn't find the file.
// TODO(hugelgupf): log this.
continue
}
return imgs, err
}
return nil, fmt.Errorf("no valid pxelinux config found")
}
func probeFiles(ethernetMac net.HardwareAddr, ip net.IP) []string {
files := make([]string, 0, 10)
// Skipping client UUID. Figure that out later.
// MAC address.
files = append(files, fmt.Sprintf("01-%s", strings.ToLower(strings.Replace(ethernetMac.String(), ":", "-", -1))))
// IP address in upper case hex, chopping one letter off at a time.
if ip != nil {
ipf := strings.ToUpper(hex.EncodeToString(ip))
for n := len(ipf); n >= 1; n-- {
files = append(files, ipf[:n])
}
}
files = append(files, "default")
return files
}