summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp/boot_image.go
blob: 88a9404ce55bb8c3ecabd6d7e334b86101b783aa (plain)
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
package bsdp

import (
	"encoding/binary"
	"fmt"
)

// BootImageType represents the different BSDP boot image types.
type BootImageType byte

// Different types of BootImages - e.g. for different flavors of macOS.
const (
	BootImageTypeMacOS9              BootImageType = 0
	BootImageTypeMacOSX              BootImageType = 1
	BootImageTypeMacOSXServer        BootImageType = 2
	BootImageTypeHardwareDiagnostics BootImageType = 3
	// 4 - 127 are reserved for future use.
)

// BootImageTypeToString maps the different BootImageTypes to human-readable
// representations.
var BootImageTypeToString = map[BootImageType]string{
	BootImageTypeMacOS9:              "macOS 9",
	BootImageTypeMacOSX:              "macOS",
	BootImageTypeMacOSXServer:        "macOS Server",
	BootImageTypeHardwareDiagnostics: "Hardware Diagnostic",
}

// BootImageID describes a boot image ID - whether it's an install image and
// what kind of boot image (e.g. OS 9, macOS, hardware diagnostics)
type BootImageID struct {
	IsInstall bool
	ImageType BootImageType
	Index     uint16
}

// ToBytes serializes a BootImageID to network-order bytes.
func (b BootImageID) ToBytes() []byte {
	bytes := make([]byte, 4)
	if b.IsInstall {
		bytes[0] |= 0x80
	}
	bytes[0] |= byte(b.ImageType)
	binary.BigEndian.PutUint16(bytes[2:], b.Index)
	return bytes
}

// String converts a BootImageID to a human-readable representation.
func (b BootImageID) String() string {
	s := fmt.Sprintf("[%d]", b.Index)
	if b.IsInstall {
		s += " installable"
	} else {
		s += " uninstallable"
	}
	t, ok := BootImageTypeToString[b.ImageType]
	if !ok {
		t = "unknown"
	}
	return s + " " + t + " image"
}

// BootImageIDFromBytes deserializes a collection of 4 bytes to a BootImageID.
func BootImageIDFromBytes(bytes []byte) (*BootImageID, error) {
	if len(bytes) < 4 {
		return nil, fmt.Errorf("not enough bytes to serialize BootImageID")
	}
	return &BootImageID{
		IsInstall: bytes[0]&0x80 != 0,
		ImageType: BootImageType(bytes[0] & 0x7f),
		Index:     binary.BigEndian.Uint16(bytes[2:]),
	}, nil
}

// BootImage describes a boot image - contains the boot image ID and the name.
type BootImage struct {
	ID   BootImageID
	Name string
}

// ToBytes converts a BootImage to a slice of bytes.
func (b *BootImage) ToBytes() []byte {
	bytes := b.ID.ToBytes()
	bytes = append(bytes, byte(len(b.Name)))
	bytes = append(bytes, []byte(b.Name)...)
	return bytes
}

// String converts a BootImage to a human-readable representation.
func (b *BootImage) String() string {
	return fmt.Sprintf("%v %v", b.Name, b.ID.String())
}

// BootImageFromBytes returns a deserialized BootImage struct from bytes.
func BootImageFromBytes(bytes []byte) (*BootImage, error) {
	// Should at least contain 4 bytes of BootImageID + byte for length of
	// boot image name.
	if len(bytes) < 5 {
		return nil, fmt.Errorf("not enough bytes to serialize BootImage")
	}
	imageID, err := BootImageIDFromBytes(bytes[:4])
	if err != nil {
		return nil, err
	}
	nameLength := int(bytes[4])
	if 5+nameLength > len(bytes) {
		return nil, fmt.Errorf("not enough bytes for BootImage")
	}
	name := string(bytes[5 : 5+nameLength])
	return &BootImage{ID: *imageID, Name: name}, nil
}