devpath.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package uefi
  15. import (
  16. "encoding/binary"
  17. "fmt"
  18. )
  19. // DevicePathType constants
  20. const (
  21. DevicePathTypeHardware = 0x01
  22. DevicePathTypeACPI = 0x02
  23. DevicePathTypeMessaging = 0x03
  24. DevicePathTypeMedia = 0x04
  25. DevicePathTypeEnd = 0x7F
  26. )
  27. // DevicePathElement represents a UEFI device path element
  28. type DevicePathElement struct {
  29. devType byte
  30. subType byte
  31. data []byte
  32. }
  33. func (e *DevicePathElement) String() string {
  34. return fmt.Sprintf("devType %v, subType %v", e.devType, e.subType)
  35. }
  36. // Type returns the device path type
  37. func (e *DevicePathElement) Type() byte {
  38. return e.devType
  39. }
  40. // SubType returns the device path subtype
  41. func (e *DevicePathElement) SubType() byte {
  42. return e.subType
  43. }
  44. // ParseDevicePathElements parses a device path from binary data
  45. func ParseDevicePathElements(data []byte) ([]*DevicePathElement, error) {
  46. if len(data) == 0 {
  47. return nil, fmt.Errorf("empty device path data")
  48. }
  49. var elements []*DevicePathElement
  50. pos := 0
  51. for pos < len(data) {
  52. // Check if we have enough data for the header
  53. if pos+4 > len(data) {
  54. return nil, fmt.Errorf("truncated device path data")
  55. }
  56. // Parse header
  57. devType := data[pos]
  58. subType := data[pos+1]
  59. length := binary.LittleEndian.Uint16(data[pos+2 : pos+4])
  60. // Validate length
  61. if length < 4 {
  62. return nil, fmt.Errorf("invalid device path element length")
  63. }
  64. // Check if we have enough data for the element
  65. if pos+int(length) > len(data) {
  66. return nil, fmt.Errorf("truncated device path element")
  67. }
  68. // Check if this is the end of the device path
  69. if devType == DevicePathTypeEnd {
  70. break
  71. }
  72. element := &DevicePathElement{
  73. devType: devType,
  74. subType: subType,
  75. data: data[pos+4 : pos+int(length)],
  76. }
  77. elements = append(elements, element)
  78. pos += int(length)
  79. }
  80. return elements, nil
  81. }