| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 3 | |
| 4 | package efivarfs |
| 5 | |
| 6 | import ( |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 7 | "bytes" |
| 8 | "encoding/binary" |
| 9 | "errors" |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 10 | "fmt" |
| 11 | "math" |
| Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 12 | "strings" |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 13 | ) |
| 14 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 15 | type LoadOptionCategory uint8 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 16 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 17 | const ( |
| 18 | // Boot entries belonging to the Boot category are normal boot entries. |
| 19 | LoadOptionCategoryBoot LoadOptionCategory = 0x0 |
| 20 | // Boot entries belonging to the App category are not booted as part of |
| 21 | // the normal boot order, but are only launched via menu or hotkey. |
| 22 | // This category is optional for bootloaders to support, before creating |
| 23 | // new boot entries of this category firmware support needs to be |
| 24 | // confirmed. |
| 25 | LoadOptionCategoryApp LoadOptionCategory = 0x1 |
| 26 | ) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 27 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 28 | // LoadOption contains information on a payload to be loaded by EFI. |
| 29 | type LoadOption struct { |
| 30 | // Human-readable description of what this load option loads. |
| 31 | // This is what's being shown by the firmware when selecting a boot option. |
| 32 | Description string |
| 33 | // If set, firmware will skip this load option when it is in BootOrder. |
| 34 | // It is unspecificed whether this prevents the user from booting the entry |
| 35 | // manually. |
| 36 | Inactive bool |
| 37 | // If set, this load option will not be shown in any menu for load option |
| 38 | // selection. This does not affect other functionality. |
| 39 | Hidden bool |
| 40 | // Category contains the category of the load entry. The selected category |
| 41 | // affects various firmware behaviors, see the individual value |
| 42 | // descriptions for more information. |
| 43 | Category LoadOptionCategory |
| 44 | // Path to the UEFI PE executable to execute when this load option is being |
| 45 | // loaded. |
| 46 | FilePath DevicePath |
| Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 47 | // ExtraPaths contains additional device paths with vendor-specific |
| 48 | // behavior. Can generally be left empty. |
| 49 | ExtraPaths []DevicePath |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 50 | // OptionalData gets passed as an argument to the executed PE executable. |
| 51 | // If zero-length a NULL value is passed to the executable. |
| 52 | OptionalData []byte |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 55 | // Marshal encodes a LoadOption into a binary EFI_LOAD_OPTION. |
| 56 | func (e *LoadOption) Marshal() ([]byte, error) { |
| 57 | var data []byte |
| 58 | var attrs uint32 |
| 59 | attrs |= (uint32(e.Category) & 0x1f) << 8 |
| 60 | if e.Hidden { |
| 61 | attrs |= 0x08 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 62 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 63 | if !e.Inactive { |
| 64 | attrs |= 0x01 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 65 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 66 | data = append32(data, attrs) |
| 67 | filePathRaw, err := e.FilePath.Marshal() |
| 68 | if err != nil { |
| 69 | return nil, fmt.Errorf("failed marshalling FilePath: %w", err) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 70 | } |
| Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 71 | for _, ep := range e.ExtraPaths { |
| 72 | epRaw, err := ep.Marshal() |
| 73 | if err != nil { |
| 74 | return nil, fmt.Errorf("failed marshalling ExtraPath: %w", err) |
| 75 | } |
| 76 | filePathRaw = append(filePathRaw, epRaw...) |
| 77 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 78 | if len(filePathRaw) > math.MaxUint16 { |
| Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 79 | return nil, fmt.Errorf("failed marshalling FilePath/ExtraPath: value too big (%d)", len(filePathRaw)) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 80 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 81 | data = append16(data, uint16(len(filePathRaw))) |
| 82 | if strings.IndexByte(e.Description, 0x00) != -1 { |
| 83 | return nil, fmt.Errorf("failed to encode Description: contains invalid null bytes") |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 84 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 85 | encodedDesc, err := Encoding.NewEncoder().Bytes([]byte(e.Description)) |
| 86 | if err != nil { |
| 87 | return nil, fmt.Errorf("failed to encode Description: %w", err) |
| 88 | } |
| 89 | data = append(data, encodedDesc...) |
| 90 | data = append(data, 0x00, 0x00) // Final UTF-16/UCS-2 null code |
| 91 | data = append(data, filePathRaw...) |
| 92 | data = append(data, e.OptionalData...) |
| 93 | return data, nil |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 96 | // UnmarshalLoadOption decodes a binary EFI_LOAD_OPTION into a LoadOption. |
| 97 | func UnmarshalLoadOption(data []byte) (*LoadOption, error) { |
| 98 | if len(data) < 6 { |
| 99 | return nil, fmt.Errorf("invalid load option: minimum 6 bytes are required, got %d", len(data)) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 100 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 101 | var opt LoadOption |
| 102 | attrs := binary.LittleEndian.Uint32(data[:4]) |
| 103 | opt.Category = LoadOptionCategory((attrs >> 8) & 0x1f) |
| 104 | opt.Hidden = attrs&0x08 != 0 |
| 105 | opt.Inactive = attrs&0x01 == 0 |
| 106 | lenPath := binary.LittleEndian.Uint16(data[4:6]) |
| 107 | // Search for UTF-16 null code |
| 108 | nullIdx := bytes.Index(data[6:], []byte{0x00, 0x00}) |
| 109 | if nullIdx == -1 { |
| 110 | return nil, errors.New("no null code point marking end of Description found") |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 111 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 112 | descriptionEnd := 6 + nullIdx + 1 |
| 113 | descriptionRaw := data[6:descriptionEnd] |
| 114 | description, err := Encoding.NewDecoder().Bytes(descriptionRaw) |
| 115 | if err != nil { |
| 116 | return nil, fmt.Errorf("error decoding UTF-16 in Description: %w", err) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 117 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 118 | descriptionEnd += 2 // 2 null bytes terminating UTF-16 string |
| 119 | opt.Description = string(description) |
| 120 | if descriptionEnd+int(lenPath) > len(data) { |
| 121 | return nil, fmt.Errorf("declared length of FilePath (%d) overruns available data (%d)", lenPath, len(data)-descriptionEnd) |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 122 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 123 | filePathData := data[descriptionEnd : descriptionEnd+int(lenPath)] |
| Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 124 | opt.FilePath, filePathData, err = UnmarshalDevicePath(filePathData) |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 125 | if err != nil { |
| 126 | return nil, fmt.Errorf("failed unmarshaling FilePath: %w", err) |
| 127 | } |
| Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 128 | for len(filePathData) > 0 { |
| 129 | var extraPath DevicePath |
| 130 | extraPath, filePathData, err = UnmarshalDevicePath(filePathData) |
| 131 | if err != nil { |
| 132 | return nil, fmt.Errorf("failed unmarshaling ExtraPath: %w", err) |
| 133 | } |
| 134 | opt.ExtraPaths = append(opt.ExtraPaths, extraPath) |
| 135 | } |
| 136 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 137 | if descriptionEnd+int(lenPath) < len(data) { |
| 138 | opt.OptionalData = data[descriptionEnd+int(lenPath):] |
| 139 | } |
| 140 | return &opt, nil |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // BootOrder represents the contents of the BootOrder EFI variable. |
| 144 | type BootOrder []uint16 |
| 145 | |
| 146 | // Marshal generates the binary representation of a BootOrder. |
| 147 | func (t *BootOrder) Marshal() []byte { |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 148 | var out []byte |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 149 | for _, v := range *t { |
| 150 | out = append16(out, v) |
| 151 | } |
| 152 | return out |
| 153 | } |
| 154 | |
| 155 | // UnmarshalBootOrder loads a BootOrder from its binary representation. |
| Lorenz Brun | 9933ef0 | 2023-07-06 18:28:29 +0200 | [diff] [blame] | 156 | func UnmarshalBootOrder(d []byte) (BootOrder, error) { |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 157 | if len(d)%2 != 0 { |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 158 | return nil, fmt.Errorf("invalid length: %v bytes", len(d)) |
| 159 | } |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 160 | l := len(d) / 2 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 161 | out := make(BootOrder, l) |
| 162 | for i := 0; i < l; i++ { |
| Lorenz Brun | 9933ef0 | 2023-07-06 18:28:29 +0200 | [diff] [blame] | 163 | out[i] = uint16(d[2*i]) | uint16(d[2*i+1])<<8 |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 164 | } |
| Lorenz Brun | 9933ef0 | 2023-07-06 18:28:29 +0200 | [diff] [blame] | 165 | return out, nil |
| Mateusz Zalega | 6cefe51 | 2021-11-08 18:19:42 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func append16(d []byte, v uint16) []byte { |
| 169 | return append(d, |
| 170 | byte(v&0xFF), |
| 171 | byte(v>>8&0xFF), |
| 172 | ) |
| 173 | } |
| 174 | |
| 175 | func append32(d []byte, v uint32) []byte { |
| 176 | return append(d, |
| 177 | byte(v&0xFF), |
| 178 | byte(v>>8&0xFF), |
| 179 | byte(v>>16&0xFF), |
| 180 | byte(v>>24&0xFF), |
| 181 | ) |
| 182 | } |