vars.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/json"
  17. "fmt"
  18. "io/ioutil"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/util/procutils"
  21. )
  22. // VarsData represents the UEFI variables data
  23. type VarsData struct {
  24. Version int `json:"version"`
  25. Variables []Variable `json:"variables"`
  26. }
  27. // Variable represents a UEFI variable
  28. type Variable struct {
  29. Name string `json:"name"`
  30. GUID string `json:"guid"`
  31. Attr int `json:"attr"`
  32. Data string `json:"data"`
  33. }
  34. // EFI_GLOBAL_VARIABLE_GUID is the GUID for EFI global variables
  35. const EFI_GLOBAL_VARIABLE_GUID = "8be4df61-93ca-11d2-aa0d-00e098032b8c"
  36. // ParseVarsJson parses UEFI variables from a JSON file
  37. func ParseVarsJson(jsonPath string) ([]*BootEntry, []uint16, error) {
  38. // Read JSON file
  39. data, err := ioutil.ReadFile(jsonPath)
  40. if err != nil {
  41. return nil, nil, fmt.Errorf("failed to read JSON file: %v", err)
  42. }
  43. // Parse JSON
  44. var varsData VarsData
  45. err = json.Unmarshal(data, &varsData)
  46. if err != nil {
  47. return nil, nil, fmt.Errorf("failed to parse JSON: %v", err)
  48. }
  49. // Parse boot entries and boot order
  50. var bootEntries []*BootEntry
  51. var bootOrder []uint16
  52. for _, v := range varsData.Variables {
  53. // Check if this is a boot entry
  54. if len(v.Name) >= 8 && v.Name[:4] == "Boot" && v.GUID == EFI_GLOBAL_VARIABLE_GUID {
  55. // Check if this is the boot order
  56. if v.Name == "BootOrder" {
  57. var err error
  58. bootOrder, err = ParseBootOrder(v.Data)
  59. if err != nil {
  60. return nil, nil, fmt.Errorf("failed to parse boot order: %v", err)
  61. }
  62. continue
  63. }
  64. // Parse boot entry
  65. name, devPaths, err := ParseBootEntryData(v.Data)
  66. if err != nil {
  67. log.Errorf("failed to parse boot entry %s: %s", v.Name, err)
  68. continue
  69. }
  70. // Create boot entry
  71. entry := &BootEntry{
  72. ID: v.Name,
  73. Name: name,
  74. DevPaths: devPaths,
  75. RawData: v.Data,
  76. }
  77. // Add entry to list
  78. bootEntries = append(bootEntries, entry)
  79. }
  80. }
  81. return bootEntries, bootOrder, nil
  82. }
  83. // UpdateBootOrderInJson updates the boot order in a UEFI variables JSON file
  84. func UpdateBootOrderInJson(jsonPath string, bootOrder []uint16) error {
  85. // Read JSON file
  86. data, err := ioutil.ReadFile(jsonPath)
  87. if err != nil {
  88. return fmt.Errorf("failed to read JSON file: %v", err)
  89. }
  90. // Parse JSON
  91. var varsData VarsData
  92. err = json.Unmarshal(data, &varsData)
  93. if err != nil {
  94. return fmt.Errorf("failed to parse JSON: %v", err)
  95. }
  96. bootOrderHex := BuildBootOrderHex(bootOrder)
  97. bootOrderFound := false
  98. for i, v := range varsData.Variables {
  99. if v.Name == "BootOrder" && v.GUID == EFI_GLOBAL_VARIABLE_GUID {
  100. varsData.Variables[i].Data = bootOrderHex
  101. bootOrderFound = true
  102. break
  103. }
  104. }
  105. // Add boot order if not found
  106. if !bootOrderFound {
  107. varsData.Variables = append(varsData.Variables, Variable{
  108. Name: "BootOrder",
  109. GUID: EFI_GLOBAL_VARIABLE_GUID,
  110. Attr: 7, // NV+BS+RT
  111. Data: bootOrderHex,
  112. })
  113. }
  114. // Write updated JSON
  115. updatedData, err := json.MarshalIndent(varsData, "", " ")
  116. if err != nil {
  117. return fmt.Errorf("failed to marshal JSON: %v", err)
  118. }
  119. err = ioutil.WriteFile(jsonPath, updatedData, 0644)
  120. if err != nil {
  121. return fmt.Errorf("failed to write JSON file: %v", err)
  122. }
  123. return nil
  124. }
  125. // ApplyJsonToVars applies a JSON file to OVMF_VARS.fd
  126. func ApplyJsonToVars(jsonPath, inputVarsPath, outputVarsPath string) ([]byte, error) {
  127. // Execute virt-fw-vars to apply JSON
  128. output, err := procutils.NewCommand("virt-fw-vars", "-i", inputVarsPath, "-o", outputVarsPath, "--set-json", jsonPath).Output()
  129. if err != nil {
  130. return output, fmt.Errorf("failed to execute virt-fw-vars --set-json command: %v", err)
  131. }
  132. return output, nil
  133. }