| 1 |
|
| 2 |
package kconfig |
| 3 |
|
| 4 |
import ( |
| 5 |
"bufio" |
| 6 |
"bytes" |
| 7 |
"compress/gzip" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"math" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/cilium/ebpf/btf" |
| 15 |
"github.com/cilium/ebpf/internal" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func Parse(source io.ReaderAt, filter map[string]struct{}) (map[string]string, error) { |
| 24 |
var r io.Reader |
| 25 |
zr, err := gzip.NewReader(io.NewSectionReader(source, 0, math.MaxInt64)) |
| 26 |
if err != nil { |
| 27 |
r = io.NewSectionReader(source, 0, math.MaxInt64) |
| 28 |
} else { |
| 29 |
|
| 30 |
r = zr |
| 31 |
} |
| 32 |
|
| 33 |
ret := make(map[string]string, len(filter)) |
| 34 |
|
| 35 |
s := bufio.NewScanner(r) |
| 36 |
|
| 37 |
for s.Scan() { |
| 38 |
line := s.Bytes() |
| 39 |
err = processKconfigLine(line, ret, filter) |
| 40 |
if err != nil { |
| 41 |
return nil, fmt.Errorf("cannot parse line: %w", err) |
| 42 |
} |
| 43 |
|
| 44 |
if filter != nil && len(ret) == len(filter) { |
| 45 |
break |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if err := s.Err(); err != nil { |
| 50 |
return nil, fmt.Errorf("cannot parse: %w", err) |
| 51 |
} |
| 52 |
|
| 53 |
if zr != nil { |
| 54 |
return ret, zr.Close() |
| 55 |
} |
| 56 |
|
| 57 |
return ret, nil |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func processKconfigLine(line []byte, m map[string]string, filter map[string]struct{}) error { |
| 64 |
|
| 65 |
if !bytes.HasPrefix(line, []byte("CONFIG_")) { |
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
key, value, found := bytes.Cut(line, []byte{'='}) |
| 70 |
if !found { |
| 71 |
return fmt.Errorf("line %q does not contain separator '='", line) |
| 72 |
} |
| 73 |
|
| 74 |
if len(value) == 0 { |
| 75 |
return fmt.Errorf("line %q has no value", line) |
| 76 |
} |
| 77 |
|
| 78 |
if filter != nil { |
| 79 |
|
| 80 |
|
| 81 |
_, ok := filter[string(key)] |
| 82 |
if !ok { |
| 83 |
return nil |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
_, ok := m[string(key)] |
| 91 |
if !ok { |
| 92 |
m[string(key)] = string(value) |
| 93 |
} |
| 94 |
|
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func PutValue(data []byte, typ btf.Type, value string) error { |
| 101 |
typ = btf.UnderlyingType(typ) |
| 102 |
|
| 103 |
switch value { |
| 104 |
case "y", "n", "m": |
| 105 |
return putValueTri(data, typ, value) |
| 106 |
} |
| 107 |
|
| 108 |
if strings.HasPrefix(value, `"`) { |
| 109 |
return putValueString(data, typ, value) |
| 110 |
} |
| 111 |
|
| 112 |
return putValueNumber(data, typ, value) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
type triState int |
| 118 |
|
| 119 |
const ( |
| 120 |
TriNo triState = 0 |
| 121 |
TriYes triState = 1 |
| 122 |
TriModule triState = 2 |
| 123 |
) |
| 124 |
|
| 125 |
func putValueTri(data []byte, typ btf.Type, value string) error { |
| 126 |
switch v := typ.(type) { |
| 127 |
case *btf.Int: |
| 128 |
if v.Encoding != btf.Bool { |
| 129 |
return fmt.Errorf("cannot add tri value, expected btf.Bool, got: %v", v.Encoding) |
| 130 |
} |
| 131 |
|
| 132 |
if v.Size != 1 { |
| 133 |
return fmt.Errorf("cannot add tri value, expected size of 1 byte, got: %d", v.Size) |
| 134 |
} |
| 135 |
|
| 136 |
switch value { |
| 137 |
case "y": |
| 138 |
data[0] = 1 |
| 139 |
case "n": |
| 140 |
data[0] = 0 |
| 141 |
default: |
| 142 |
return fmt.Errorf("cannot use %q for btf.Bool", value) |
| 143 |
} |
| 144 |
case *btf.Enum: |
| 145 |
if v.Name != "libbpf_tristate" { |
| 146 |
return fmt.Errorf("cannot use enum %q, only libbpf_tristate is supported", v.Name) |
| 147 |
} |
| 148 |
|
| 149 |
if len(data) != 4 { |
| 150 |
return fmt.Errorf("expected enum value to occupy 4 bytes in datasec, got: %d", len(data)) |
| 151 |
} |
| 152 |
|
| 153 |
var tri triState |
| 154 |
switch value { |
| 155 |
case "y": |
| 156 |
tri = TriYes |
| 157 |
case "m": |
| 158 |
tri = TriModule |
| 159 |
case "n": |
| 160 |
tri = TriNo |
| 161 |
default: |
| 162 |
return fmt.Errorf("value %q is not supported for libbpf_tristate", value) |
| 163 |
} |
| 164 |
|
| 165 |
internal.NativeEndian.PutUint32(data, uint32(tri)) |
| 166 |
default: |
| 167 |
return fmt.Errorf("cannot add number value, expected btf.Int or btf.Enum, got: %T", v) |
| 168 |
} |
| 169 |
|
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
func putValueString(data []byte, typ btf.Type, value string) error { |
| 174 |
array, ok := typ.(*btf.Array) |
| 175 |
if !ok { |
| 176 |
return fmt.Errorf("cannot add string value, expected btf.Array, got %T", array) |
| 177 |
} |
| 178 |
|
| 179 |
contentType, ok := btf.UnderlyingType(array.Type).(*btf.Int) |
| 180 |
if !ok { |
| 181 |
return fmt.Errorf("cannot add string value, expected array of btf.Int, got %T", contentType) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if contentType.Size != 1 || contentType.Encoding == btf.Bool { |
| 187 |
return fmt.Errorf("cannot add string value, expected array of non-bool btf.Int of size 1, got array of btf.Int with encoding %v and size %v", contentType.Encoding, contentType.Size) |
| 188 |
} |
| 189 |
|
| 190 |
if !strings.HasPrefix(value, `"`) || !strings.HasSuffix(value, `"`) { |
| 191 |
return fmt.Errorf(`value %q must start and finish with '"'`, value) |
| 192 |
} |
| 193 |
|
| 194 |
str := strings.Trim(value, `"`) |
| 195 |
|
| 196 |
|
| 197 |
if uint32(len(str)) >= array.Nelems { |
| 198 |
str = str[:array.Nelems] |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
copy(data, str) |
| 203 |
|
| 204 |
return nil |
| 205 |
} |
| 206 |
|
| 207 |
func putValueNumber(data []byte, typ btf.Type, value string) error { |
| 208 |
integer, ok := typ.(*btf.Int) |
| 209 |
if !ok { |
| 210 |
return fmt.Errorf("cannot add number value, expected *btf.Int, got: %T", integer) |
| 211 |
} |
| 212 |
|
| 213 |
size := integer.Size |
| 214 |
sizeInBits := size * 8 |
| 215 |
|
| 216 |
var n uint64 |
| 217 |
var err error |
| 218 |
if integer.Encoding == btf.Signed { |
| 219 |
parsed, e := strconv.ParseInt(value, 0, int(sizeInBits)) |
| 220 |
|
| 221 |
n = uint64(parsed) |
| 222 |
err = e |
| 223 |
} else { |
| 224 |
parsed, e := strconv.ParseUint(value, 0, int(sizeInBits)) |
| 225 |
|
| 226 |
n = uint64(parsed) |
| 227 |
err = e |
| 228 |
} |
| 229 |
|
| 230 |
if err != nil { |
| 231 |
return fmt.Errorf("cannot parse value: %w", err) |
| 232 |
} |
| 233 |
|
| 234 |
return PutInteger(data, integer, n) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
func PutInteger(data []byte, integer *btf.Int, n uint64) error { |
| 242 |
|
| 243 |
if integer.Encoding == btf.Bool && n > 1 { |
| 244 |
return fmt.Errorf("invalid boolean value: %d", n) |
| 245 |
} |
| 246 |
|
| 247 |
if len(data) < int(integer.Size) { |
| 248 |
return fmt.Errorf("can't fit an integer of size %d into a byte slice of length %d", integer.Size, len(data)) |
| 249 |
} |
| 250 |
|
| 251 |
switch integer.Size { |
| 252 |
case 1: |
| 253 |
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt8 || int64(n) < math.MinInt8) { |
| 254 |
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size) |
| 255 |
} |
| 256 |
data[0] = byte(n) |
| 257 |
case 2: |
| 258 |
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt16 || int64(n) < math.MinInt16) { |
| 259 |
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size) |
| 260 |
} |
| 261 |
internal.NativeEndian.PutUint16(data, uint16(n)) |
| 262 |
case 4: |
| 263 |
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt32 || int64(n) < math.MinInt32) { |
| 264 |
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size) |
| 265 |
} |
| 266 |
internal.NativeEndian.PutUint32(data, uint32(n)) |
| 267 |
case 8: |
| 268 |
internal.NativeEndian.PutUint64(data, uint64(n)) |
| 269 |
default: |
| 270 |
return fmt.Errorf("size (%d) is not valid, expected: 1, 2, 4 or 8", integer.Size) |
| 271 |
} |
| 272 |
|
| 273 |
return nil |
| 274 |
} |