| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
type PCIAddr struct { |
| 17 |
Bus int |
| 18 |
Dev int |
| 19 |
Func int |
| 20 |
} |
| 21 |
|
| 22 |
type PCIDevData struct { |
| 23 |
PCIAddr |
| 24 |
PCIVenID uint16 |
| 25 |
PCIDevID uint16 |
| 26 |
ConfigDump []uint8 |
| 27 |
} |
| 28 |
|
| 29 |
type PCIDevice interface { |
| 30 |
Scan(ctx Context, addr PCIDevData) |
| 31 |
} |
| 32 |
|
| 33 |
type InteltoolData struct { |
| 34 |
GPIO map[uint16]uint32 |
| 35 |
RCBA map[uint16]uint32 |
| 36 |
IOBP map[uint32]uint32 |
| 37 |
IGD map[uint32]uint32 |
| 38 |
MCHBAR map[uint16]uint32 |
| 39 |
PMBASE map[uint16]uint32 |
| 40 |
} |
| 41 |
|
| 42 |
type DMIData struct { |
| 43 |
Vendor string |
| 44 |
Model string |
| 45 |
Version string |
| 46 |
IsLaptop bool |
| 47 |
} |
| 48 |
|
| 49 |
type AzaliaCodec struct { |
| 50 |
Name string |
| 51 |
VendorID uint32 |
| 52 |
SubsystemID uint32 |
| 53 |
CodecNo int |
| 54 |
PinConfig map[int]uint32 |
| 55 |
} |
| 56 |
|
| 57 |
type DevReader interface { |
| 58 |
GetPCIList() []PCIDevData |
| 59 |
GetDMI() DMIData |
| 60 |
GetInteltool() InteltoolData |
| 61 |
GetAzaliaCodecs() []AzaliaCodec |
| 62 |
GetACPI() map[string][]byte |
| 63 |
GetCPUModel() []uint32 |
| 64 |
GetEC() []byte |
| 65 |
GetIOPorts() []IOPorts |
| 66 |
HasPS2() bool |
| 67 |
} |
| 68 |
|
| 69 |
type IOPorts struct { |
| 70 |
Start uint16 |
| 71 |
End uint16 |
| 72 |
Usage string |
| 73 |
} |
| 74 |
|
| 75 |
type SouthBridger interface { |
| 76 |
GetGPIOHeader() string |
| 77 |
EncodeGPE(int) int |
| 78 |
DecodeGPE(int) int |
| 79 |
EnableGPE(int) |
| 80 |
NeedRouteGPIOManually() |
| 81 |
} |
| 82 |
|
| 83 |
var SouthBridge SouthBridger |
| 84 |
var BootBlockFiles map[string]string = map[string]string{} |
| 85 |
var ROMStageFiles map[string]string = map[string]string{} |
| 86 |
var RAMStageFiles map[string]string = map[string]string{} |
| 87 |
var SMMFiles map[string]string = map[string]string{} |
| 88 |
var MainboardInit string |
| 89 |
var MainboardEnable string |
| 90 |
var MainboardIncludes []string |
| 91 |
|
| 92 |
type Context struct { |
| 93 |
MoboID string |
| 94 |
KconfigName string |
| 95 |
Vendor string |
| 96 |
Model string |
| 97 |
BaseDirectory string |
| 98 |
InfoSource DevReader |
| 99 |
SaneVendor string |
| 100 |
} |
| 101 |
|
| 102 |
type Filetype int |
| 103 |
|
| 104 |
const ( |
| 105 |
Ada Filetype = iota |
| 106 |
ASL |
| 107 |
C |
| 108 |
Kconfig |
| 109 |
Makefile |
| 110 |
) |
| 111 |
|
| 112 |
var CommentFormatStrings map[Filetype]string = map[Filetype]string { |
| 113 |
Ada: "-- %s\n", |
| 114 |
ASL: "/* %s */\n", |
| 115 |
C: "/* %s */\n", |
| 116 |
Kconfig: "## %s\n", |
| 117 |
Makefile: "## %s\n", |
| 118 |
} |
| 119 |
|
| 120 |
type License string |
| 121 |
|
| 122 |
const ( |
| 123 |
CC_PDDC License = "CC-PDDC" |
| 124 |
GPL2_only = "GPL-2.0-only" |
| 125 |
GPL2_or_later = "GPL-2.0-or-later" |
| 126 |
) |
| 127 |
|
| 128 |
var KconfigBool map[string]bool = map[string]bool{} |
| 129 |
var KconfigComment map[string]string = map[string]string{} |
| 130 |
var KconfigString map[string]string = map[string]string{} |
| 131 |
var KconfigHex map[string]uint32 = map[string]uint32{} |
| 132 |
var KconfigInt map[string]int = map[string]int{} |
| 133 |
var ROMSizeKB = 0 |
| 134 |
var ROMProtocol = "" |
| 135 |
var FlashROMSupport = "" |
| 136 |
|
| 137 |
func GetLE16(inp []byte) uint16 { |
| 138 |
return uint16(inp[0]) | (uint16(inp[1]) << 8) |
| 139 |
} |
| 140 |
|
| 141 |
func FormatHexLE16(inp []byte) string { |
| 142 |
return fmt.Sprintf("0x%04x", GetLE16(inp)) |
| 143 |
} |
| 144 |
|
| 145 |
func FormatHex32(u uint32) string { |
| 146 |
return fmt.Sprintf("0x%08x", u) |
| 147 |
} |
| 148 |
|
| 149 |
func FormatHex8(u uint8) string { |
| 150 |
return fmt.Sprintf("0x%02x", u) |
| 151 |
} |
| 152 |
|
| 153 |
func FormatInt32(u uint32) string { |
| 154 |
return fmt.Sprintf("%d", u) |
| 155 |
} |
| 156 |
|
| 157 |
func FormatHexLE32(d []uint8) string { |
| 158 |
u := uint32(d[0]) | (uint32(d[1]) << 8) | (uint32(d[2]) << 16) | (uint32(d[3]) << 24) |
| 159 |
return FormatHex32(u) |
| 160 |
} |
| 161 |
|
| 162 |
func FormatBool(inp bool) string { |
| 163 |
if inp { |
| 164 |
return "1" |
| 165 |
} else { |
| 166 |
return "0" |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func sanitize(inp string) string { |
| 171 |
result := strings.ToLower(inp) |
| 172 |
result = strings.Replace(result, " ", "_", -1) |
| 173 |
result = strings.Replace(result, ",", "_", -1) |
| 174 |
result = strings.Replace(result, "-", "_", -1) |
| 175 |
for strings.HasSuffix(result, ".") { |
| 176 |
result = result[0 : len(result)-1] |
| 177 |
} |
| 178 |
return result |
| 179 |
} |
| 180 |
|
| 181 |
func AddBootBlockFile(Name string, Condition string) { |
| 182 |
BootBlockFiles[Name] = Condition |
| 183 |
} |
| 184 |
|
| 185 |
func AddROMStageFile(Name string, Condition string) { |
| 186 |
ROMStageFiles[Name] = Condition |
| 187 |
} |
| 188 |
|
| 189 |
func AddRAMStageFile(Name string, Condition string) { |
| 190 |
RAMStageFiles[Name] = Condition |
| 191 |
} |
| 192 |
|
| 193 |
func AddSMMFile(Name string, Condition string) { |
| 194 |
SMMFiles[Name] = Condition |
| 195 |
} |
| 196 |
|
| 197 |
func IsIOPortUsedBy(ctx Context, port uint16, name string) bool { |
| 198 |
for _, io := range ctx.InfoSource.GetIOPorts() { |
| 199 |
if io.Start <= port && port <= io.End && io.Usage == name { |
| 200 |
return true |
| 201 |
} |
| 202 |
} |
| 203 |
return false |
| 204 |
} |
| 205 |
|
| 206 |
var FlagOutDir = flag.String("coreboot_dir", ".", "Resulting coreboot directory") |
| 207 |
|
| 208 |
func writeMF(mf *os.File, files map[string]string, category string) { |
| 209 |
keys := []string{} |
| 210 |
for file, _ := range files { |
| 211 |
keys = append(keys, file) |
| 212 |
} |
| 213 |
|
| 214 |
sort.Strings(keys) |
| 215 |
|
| 216 |
for _, file := range keys { |
| 217 |
condition := files[file] |
| 218 |
if condition == "" { |
| 219 |
fmt.Fprintf(mf, "%s-y += %s\n", category, file) |
| 220 |
} else { |
| 221 |
fmt.Fprintf(mf, "%s-$(%s) += %s\n", category, condition, file) |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func Create(ctx Context, name string) *os.File { |
| 227 |
li := strings.LastIndex(name, "/") |
| 228 |
if li > 0 { |
| 229 |
os.MkdirAll(ctx.BaseDirectory+"/"+name[0:li], 0700) |
| 230 |
} |
| 231 |
mf, err := os.Create(ctx.BaseDirectory + "/" + name) |
| 232 |
if err != nil { |
| 233 |
log.Fatal(err) |
| 234 |
} |
| 235 |
return mf |
| 236 |
} |
| 237 |
func Create_Empty(ctx Context, name string, filetype Filetype) { |
| 238 |
f := Create(ctx, name) |
| 239 |
defer f.Close() |
| 240 |
Add_SPDX(f, filetype, CC_PDDC) |
| 241 |
Add_Comment(f, filetype, |
| 242 |
"Please update the license if adding licensable material.") |
| 243 |
} |
| 244 |
|
| 245 |
func Add_SPDX(f *os.File, filetype Filetype, license License) { |
| 246 |
Add_Comment(f, filetype, "SPDX-License-Identifier: " + string(license)) |
| 247 |
fmt.Fprintln(f) |
| 248 |
} |
| 249 |
|
| 250 |
func Add_Comment(f *os.File, filetype Filetype, comment string) { |
| 251 |
fmt.Fprintf(f, CommentFormatStrings[filetype], comment) |
| 252 |
} |
| 253 |
|
| 254 |
func RestorePCI16Simple(f *os.File, pcidev PCIDevData, addr uint16) { |
| 255 |
fmt.Fprintf(f, " pci_write_config16(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x);\n", |
| 256 |
pcidev.Bus, pcidev.Dev, pcidev.Func, addr, |
| 257 |
pcidev.ConfigDump[addr+1], |
| 258 |
pcidev.ConfigDump[addr]) |
| 259 |
} |
| 260 |
|
| 261 |
func RestorePCI32Simple(f *os.File, pcidev PCIDevData, addr uint16) { |
| 262 |
fmt.Fprintf(f, " pci_write_config32(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x%02x%02x);\n", |
| 263 |
pcidev.Bus, pcidev.Dev, pcidev.Func, addr, |
| 264 |
pcidev.ConfigDump[addr+3], |
| 265 |
pcidev.ConfigDump[addr+2], |
| 266 |
pcidev.ConfigDump[addr+1], |
| 267 |
pcidev.ConfigDump[addr]) |
| 268 |
} |
| 269 |
|
| 270 |
func RestoreRCBA32(f *os.File, inteltool InteltoolData, addr uint16) { |
| 271 |
fmt.Fprintf(f, "\tRCBA32(0x%04x) = 0x%08x;\n", addr, inteltool.RCBA[addr]) |
| 272 |
} |
| 273 |
|
| 274 |
type PCISlot struct { |
| 275 |
PCIAddr |
| 276 |
alias string |
| 277 |
additionalComment string |
| 278 |
writeEmpty bool |
| 279 |
} |
| 280 |
|
| 281 |
type DevTreeNode struct { |
| 282 |
Bus int |
| 283 |
Dev int |
| 284 |
Func int |
| 285 |
Disabled bool |
| 286 |
Registers map[string]string |
| 287 |
IOs map[uint16]uint16 |
| 288 |
Children []DevTreeNode |
| 289 |
PCISlots []PCISlot |
| 290 |
PCIController bool |
| 291 |
ChildPCIBus int |
| 292 |
MissingParent string |
| 293 |
SubVendor uint16 |
| 294 |
SubSystem uint16 |
| 295 |
Chip string |
| 296 |
Ops string |
| 297 |
Comment string |
| 298 |
} |
| 299 |
|
| 300 |
var DevTree DevTreeNode |
| 301 |
var MissingChildren map[string][]DevTreeNode = map[string][]DevTreeNode{} |
| 302 |
var unmatchedPCIChips map[PCIAddr]DevTreeNode = map[PCIAddr]DevTreeNode{} |
| 303 |
var unmatchedPCIDevices map[PCIAddr]DevTreeNode = map[PCIAddr]DevTreeNode{} |
| 304 |
|
| 305 |
func Offset(dt *os.File, offset int) { |
| 306 |
for i := 0; i < offset; i++ { |
| 307 |
fmt.Fprintf(dt, "\t") |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func MatchDev(dev *DevTreeNode) { |
| 312 |
for idx := range dev.Children { |
| 313 |
MatchDev(&dev.Children[idx]) |
| 314 |
} |
| 315 |
|
| 316 |
for _, slot := range dev.PCISlots { |
| 317 |
slotDev, ok := unmatchedPCIDevices[slot.PCIAddr] |
| 318 |
if !ok { |
| 319 |
if slot.writeEmpty { |
| 320 |
dev.Children = append(dev.Children, |
| 321 |
DevTreeNode{ |
| 322 |
Registers: map[string]string{}, |
| 323 |
Chip: "pci", |
| 324 |
Bus: slot.Bus, |
| 325 |
Dev: slot.Dev, |
| 326 |
Func: slot.Func, |
| 327 |
Comment: slot.additionalComment, |
| 328 |
Disabled: true, |
| 329 |
}, |
| 330 |
) |
| 331 |
} |
| 332 |
continue |
| 333 |
} |
| 334 |
|
| 335 |
if slot.additionalComment != "" && slotDev.Comment != "" { |
| 336 |
slotDev.Comment = slot.additionalComment + " " + slotDev.Comment |
| 337 |
} else { |
| 338 |
slotDev.Comment = slot.additionalComment + slotDev.Comment |
| 339 |
} |
| 340 |
|
| 341 |
MatchDev(&slotDev) |
| 342 |
dev.Children = append(dev.Children, slotDev) |
| 343 |
delete(unmatchedPCIDevices, slot.PCIAddr) |
| 344 |
} |
| 345 |
|
| 346 |
for _, slot := range dev.PCISlots { |
| 347 |
slotChip, ok := unmatchedPCIChips[slot.PCIAddr] |
| 348 |
|
| 349 |
if !ok { |
| 350 |
continue |
| 351 |
} |
| 352 |
|
| 353 |
if slot.additionalComment != "" && slotChip.Comment != "" { |
| 354 |
slotChip.Comment = slot.additionalComment + " " + slotChip.Comment |
| 355 |
} else { |
| 356 |
slotChip.Comment = slot.additionalComment + slotChip.Comment |
| 357 |
} |
| 358 |
|
| 359 |
delete(unmatchedPCIChips, slot.PCIAddr) |
| 360 |
MatchDev(&slotChip) |
| 361 |
dev.Children = append(dev.Children, slotChip) |
| 362 |
} |
| 363 |
|
| 364 |
if dev.PCIController { |
| 365 |
for slot, slotDev := range unmatchedPCIChips { |
| 366 |
if slot.Bus == dev.ChildPCIBus { |
| 367 |
delete(unmatchedPCIChips, slot) |
| 368 |
MatchDev(&slotDev) |
| 369 |
dev.Children = append(dev.Children, slotDev) |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if dev.MissingParent != "" { |
| 375 |
for _, child := range MissingChildren[dev.MissingParent] { |
| 376 |
MatchDev(&child) |
| 377 |
dev.Children = append(dev.Children, child) |
| 378 |
} |
| 379 |
delete(MissingChildren, dev.MissingParent) |
| 380 |
} |
| 381 |
|
| 382 |
if dev.PCIController { |
| 383 |
for slot, slotDev := range unmatchedPCIDevices { |
| 384 |
if slot.Bus == dev.ChildPCIBus { |
| 385 |
MatchDev(&slotDev) |
| 386 |
dev.Children = append(dev.Children, slotDev) |
| 387 |
delete(unmatchedPCIDevices, slot) |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
func writeOn(dt *os.File, dev DevTreeNode) { |
| 394 |
if dev.Disabled { |
| 395 |
fmt.Fprintf(dt, "off") |
| 396 |
} else { |
| 397 |
fmt.Fprintf(dt, "on") |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
func WriteDev(dt *os.File, offset int, alias string, dev DevTreeNode) { |
| 402 |
Offset(dt, offset) |
| 403 |
switch dev.Chip { |
| 404 |
case "cpu_cluster", "domain": |
| 405 |
fmt.Fprintf(dt, "device %s 0x%x ", dev.Chip, dev.Dev) |
| 406 |
writeOn(dt, dev) |
| 407 |
case "pci", "pnp": |
| 408 |
if alias != "" { |
| 409 |
fmt.Fprintf(dt, "device ref %s ", alias) |
| 410 |
} else { |
| 411 |
fmt.Fprintf(dt, "device %s %02x.%x ", dev.Chip, dev.Dev, dev.Func) |
| 412 |
} |
| 413 |
writeOn(dt, dev) |
| 414 |
case "i2c": |
| 415 |
fmt.Fprintf(dt, "device %s %02x ", dev.Chip, dev.Dev) |
| 416 |
writeOn(dt, dev) |
| 417 |
default: |
| 418 |
fmt.Fprintf(dt, "chip %s", dev.Chip) |
| 419 |
} |
| 420 |
if dev.Comment != "" { |
| 421 |
fmt.Fprintf(dt, " # %s", dev.Comment) |
| 422 |
} |
| 423 |
fmt.Fprintf(dt, "\n") |
| 424 |
if dev.Ops != "" { |
| 425 |
Offset(dt, offset+1) |
| 426 |
fmt.Fprintf(dt, "ops %s\n", dev.Ops) |
| 427 |
} |
| 428 |
if dev.Chip == "pci" && dev.SubSystem != 0 && dev.SubVendor != 0 { |
| 429 |
Offset(dt, offset+1) |
| 430 |
fmt.Fprintf(dt, "subsystemid 0x%04x 0x%04x\n", dev.SubVendor, dev.SubSystem) |
| 431 |
} |
| 432 |
|
| 433 |
keys := []string{} |
| 434 |
for reg, _ := range dev.Registers { |
| 435 |
keys = append(keys, reg) |
| 436 |
} |
| 437 |
|
| 438 |
sort.Strings(keys) |
| 439 |
|
| 440 |
for _, reg := range keys { |
| 441 |
val := dev.Registers[reg] |
| 442 |
Offset(dt, offset+1) |
| 443 |
fmt.Fprintf(dt, "register \"%s\" = \"%s\"\n", reg, val) |
| 444 |
} |
| 445 |
|
| 446 |
ios := []int{} |
| 447 |
for reg, _ := range dev.IOs { |
| 448 |
ios = append(ios, int(reg)) |
| 449 |
} |
| 450 |
|
| 451 |
sort.Ints(ios) |
| 452 |
|
| 453 |
for _, reg := range ios { |
| 454 |
val := dev.IOs[uint16(reg)] |
| 455 |
Offset(dt, offset+1) |
| 456 |
fmt.Fprintf(dt, "io 0x%x = 0x%x\n", reg, val) |
| 457 |
} |
| 458 |
|
| 459 |
for _, child := range dev.Children { |
| 460 |
alias = "" |
| 461 |
for _, slot := range dev.PCISlots { |
| 462 |
if slot.PCIAddr.Bus == child.Bus && |
| 463 |
slot.PCIAddr.Dev == child.Dev && slot.PCIAddr.Func == child.Func { |
| 464 |
alias = slot.alias |
| 465 |
} |
| 466 |
} |
| 467 |
WriteDev(dt, offset+1, alias, child) |
| 468 |
} |
| 469 |
|
| 470 |
Offset(dt, offset) |
| 471 |
fmt.Fprintf(dt, "end\n") |
| 472 |
} |
| 473 |
|
| 474 |
func PutChip(domain string, cur DevTreeNode) { |
| 475 |
MissingChildren[domain] = append(MissingChildren[domain], cur) |
| 476 |
} |
| 477 |
|
| 478 |
func PutPCIChip(addr PCIDevData, cur DevTreeNode) { |
| 479 |
unmatchedPCIChips[addr.PCIAddr] = cur |
| 480 |
} |
| 481 |
|
| 482 |
func PutPCIDevParent(addr PCIDevData, comment string, parent string) { |
| 483 |
cur := DevTreeNode{ |
| 484 |
Registers: map[string]string{}, |
| 485 |
Chip: "pci", |
| 486 |
Bus: addr.Bus, |
| 487 |
Dev: addr.Dev, |
| 488 |
Func: addr.Func, |
| 489 |
MissingParent: parent, |
| 490 |
Comment: comment, |
| 491 |
} |
| 492 |
if addr.ConfigDump[0xa] == 0x04 && addr.ConfigDump[0xb] == 0x06 { |
| 493 |
cur.PCIController = true |
| 494 |
cur.ChildPCIBus = int(addr.ConfigDump[0x19]) |
| 495 |
|
| 496 |
loopCtr := 0 |
| 497 |
for capPtr := addr.ConfigDump[0x34]; capPtr != 0; capPtr = addr.ConfigDump[capPtr+1] { |
| 498 |
|
| 499 |
|
| 500 |
loopCtr++ |
| 501 |
if loopCtr > 0x100 { |
| 502 |
break |
| 503 |
} |
| 504 |
if addr.ConfigDump[capPtr] == 0x0d { |
| 505 |
cur.SubVendor = GetLE16(addr.ConfigDump[capPtr+4 : capPtr+6]) |
| 506 |
cur.SubSystem = GetLE16(addr.ConfigDump[capPtr+6 : capPtr+8]) |
| 507 |
} |
| 508 |
} |
| 509 |
} else { |
| 510 |
cur.SubVendor = GetLE16(addr.ConfigDump[0x2c:0x2e]) |
| 511 |
cur.SubSystem = GetLE16(addr.ConfigDump[0x2e:0x30]) |
| 512 |
} |
| 513 |
unmatchedPCIDevices[addr.PCIAddr] = cur |
| 514 |
} |
| 515 |
|
| 516 |
func PutPCIDev(addr PCIDevData, comment string) { |
| 517 |
PutPCIDevParent(addr, comment, "") |
| 518 |
} |
| 519 |
|
| 520 |
type GenericPCI struct { |
| 521 |
Comment string |
| 522 |
Bus0Subdiv string |
| 523 |
MissingParent string |
| 524 |
} |
| 525 |
|
| 526 |
type GenericVGA struct { |
| 527 |
GenericPCI |
| 528 |
} |
| 529 |
|
| 530 |
type DSDTInclude struct { |
| 531 |
Comment string |
| 532 |
File string |
| 533 |
} |
| 534 |
|
| 535 |
type DSDTDefine struct { |
| 536 |
Key string |
| 537 |
Comment string |
| 538 |
Value string |
| 539 |
} |
| 540 |
|
| 541 |
var DSDTIncludes []DSDTInclude |
| 542 |
var DSDTPCI0Includes []DSDTInclude |
| 543 |
var DSDTDefines []DSDTDefine |
| 544 |
|
| 545 |
func (g GenericPCI) Scan(ctx Context, addr PCIDevData) { |
| 546 |
PutPCIDevParent(addr, g.Comment, g.MissingParent) |
| 547 |
} |
| 548 |
|
| 549 |
var IGDEnabled bool = false |
| 550 |
|
| 551 |
func (g GenericVGA) Scan(ctx Context, addr PCIDevData) { |
| 552 |
KconfigString["VGA_BIOS_ID"] = fmt.Sprintf("%04x,%04x", |
| 553 |
addr.PCIVenID, |
| 554 |
addr.PCIDevID) |
| 555 |
PutPCIDevParent(addr, g.Comment, g.MissingParent) |
| 556 |
IGDEnabled = true |
| 557 |
} |
| 558 |
|
| 559 |
func makeKconfigName(ctx Context) { |
| 560 |
kn := Create(ctx, "Kconfig.name") |
| 561 |
defer kn.Close() |
| 562 |
Add_SPDX(kn, Kconfig, GPL2_only) |
| 563 |
|
| 564 |
fmt.Fprintf(kn, "config %s\n\tbool \"%s\"\n", ctx.KconfigName, ctx.Model) |
| 565 |
} |
| 566 |
|
| 567 |
func makeComment(name string) string { |
| 568 |
cmt, ok := KconfigComment[name] |
| 569 |
if !ok { |
| 570 |
return "" |
| 571 |
} |
| 572 |
return " # " + cmt |
| 573 |
} |
| 574 |
|
| 575 |
func makeKconfig(ctx Context) { |
| 576 |
kc := Create(ctx, "Kconfig") |
| 577 |
defer kc.Close() |
| 578 |
Add_SPDX(kc, Kconfig, GPL2_only) |
| 579 |
|
| 580 |
fmt.Fprintf(kc, "if %s\n\n", ctx.KconfigName) |
| 581 |
|
| 582 |
fmt.Fprintf(kc, "config BOARD_SPECIFIC_OPTIONS\n\tdef_bool y\n") |
| 583 |
keys := []string{} |
| 584 |
for name, val := range KconfigBool { |
| 585 |
if val { |
| 586 |
keys = append(keys, name) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
sort.Strings(keys) |
| 591 |
|
| 592 |
for _, name := range keys { |
| 593 |
fmt.Fprintf(kc, "\tselect %s%s\n", name, makeComment(name)) |
| 594 |
} |
| 595 |
|
| 596 |
keys = nil |
| 597 |
for name, val := range KconfigBool { |
| 598 |
if !val { |
| 599 |
keys = append(keys, name) |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
sort.Strings(keys) |
| 604 |
|
| 605 |
for _, name := range keys { |
| 606 |
fmt.Fprintf(kc, ` |
| 607 |
config %s%s |
| 608 |
bool |
| 609 |
default n |
| 610 |
`, name, makeComment(name)) |
| 611 |
} |
| 612 |
|
| 613 |
keys = nil |
| 614 |
for name, _ := range KconfigString { |
| 615 |
keys = append(keys, name) |
| 616 |
} |
| 617 |
|
| 618 |
sort.Strings(keys) |
| 619 |
|
| 620 |
for _, name := range keys { |
| 621 |
fmt.Fprintf(kc, ` |
| 622 |
config %s%s |
| 623 |
string |
| 624 |
default "%s" |
| 625 |
`, name, makeComment(name), KconfigString[name]) |
| 626 |
} |
| 627 |
|
| 628 |
keys = nil |
| 629 |
for name, _ := range KconfigHex { |
| 630 |
keys = append(keys, name) |
| 631 |
} |
| 632 |
|
| 633 |
sort.Strings(keys) |
| 634 |
|
| 635 |
for _, name := range keys { |
| 636 |
fmt.Fprintf(kc, ` |
| 637 |
config %s%s |
| 638 |
hex |
| 639 |
default 0x%x |
| 640 |
`, name, makeComment(name), KconfigHex[name]) |
| 641 |
} |
| 642 |
|
| 643 |
keys = nil |
| 644 |
for name, _ := range KconfigInt { |
| 645 |
keys = append(keys, name) |
| 646 |
} |
| 647 |
|
| 648 |
sort.Strings(keys) |
| 649 |
|
| 650 |
for _, name := range keys { |
| 651 |
fmt.Fprintf(kc, ` |
| 652 |
config %s%s |
| 653 |
int |
| 654 |
default %d |
| 655 |
`, name, makeComment(name), KconfigInt[name]) |
| 656 |
} |
| 657 |
|
| 658 |
fmt.Fprintf(kc, "endif\n") |
| 659 |
} |
| 660 |
|
| 661 |
const MoboDir = "/src/mainboard/" |
| 662 |
|
| 663 |
func makeVendor(ctx Context) { |
| 664 |
vendor := ctx.Vendor |
| 665 |
vendorSane := ctx.SaneVendor |
| 666 |
vendorDir := *FlagOutDir + MoboDir + vendorSane |
| 667 |
vendorUpper := strings.ToUpper(vendorSane) |
| 668 |
kconfig := vendorDir + "/Kconfig" |
| 669 |
if _, err := os.Stat(kconfig); os.IsNotExist(err) { |
| 670 |
f, err := os.Create(kconfig) |
| 671 |
if err != nil { |
| 672 |
log.Fatal(err) |
| 673 |
} |
| 674 |
defer f.Close() |
| 675 |
Add_SPDX(f, Kconfig, GPL2_only) |
| 676 |
f.WriteString(`if VENDOR_` + vendorUpper + ` |
| 677 |
|
| 678 |
choice |
| 679 |
prompt "Mainboard model" |
| 680 |
|
| 681 |
source "src/mainboard/` + vendorSane + `/*/Kconfig.name" |
| 682 |
|
| 683 |
endchoice |
| 684 |
|
| 685 |
source "src/mainboard/` + vendorSane + `/*/Kconfig" |
| 686 |
|
| 687 |
config MAINBOARD_VENDOR |
| 688 |
string |
| 689 |
default "` + vendor + `" |
| 690 |
|
| 691 |
endif # VENDOR_` + vendorUpper + "\n") |
| 692 |
} |
| 693 |
kconfigName := vendorDir + "/Kconfig.name" |
| 694 |
if _, err := os.Stat(kconfigName); os.IsNotExist(err) { |
| 695 |
f, err := os.Create(kconfigName) |
| 696 |
if err != nil { |
| 697 |
log.Fatal(err) |
| 698 |
} |
| 699 |
defer f.Close() |
| 700 |
Add_SPDX(f, Kconfig, GPL2_only) |
| 701 |
f.WriteString(`config VENDOR_` + vendorUpper + ` |
| 702 |
bool "` + vendor + `" |
| 703 |
`) |
| 704 |
} |
| 705 |
|
| 706 |
} |
| 707 |
|
| 708 |
func GuessECGPE(ctx Context) int { |
| 709 |
|
| 710 |
dsdt := ctx.InfoSource.GetACPI()["DSDT"] |
| 711 |
idx := bytes.Index(dsdt, []byte{0x08, '_', 'G', 'P', 'E', 0x0a}) |
| 712 |
if idx > 0 { |
| 713 |
return int(dsdt[idx+6]) |
| 714 |
} |
| 715 |
return -1 |
| 716 |
} |
| 717 |
|
| 718 |
func GuessSPDMap(ctx Context) []uint8 { |
| 719 |
dmi := ctx.InfoSource.GetDMI() |
| 720 |
|
| 721 |
if dmi.Vendor == "LENOVO" { |
| 722 |
return []uint8{0x50, 0x52, 0x51, 0x53} |
| 723 |
} |
| 724 |
return []uint8{0x50, 0x51, 0x52, 0x53} |
| 725 |
} |
| 726 |
|
| 727 |
func main() { |
| 728 |
flag.Parse() |
| 729 |
|
| 730 |
ctx := Context{} |
| 731 |
|
| 732 |
ctx.InfoSource = MakeLogReader() |
| 733 |
|
| 734 |
dmi := ctx.InfoSource.GetDMI() |
| 735 |
|
| 736 |
ctx.Vendor = dmi.Vendor |
| 737 |
|
| 738 |
if dmi.Vendor == "LENOVO" { |
| 739 |
ctx.Model = dmi.Version |
| 740 |
} else { |
| 741 |
ctx.Model = dmi.Model |
| 742 |
} |
| 743 |
|
| 744 |
if dmi.IsLaptop { |
| 745 |
KconfigBool["SYSTEM_TYPE_LAPTOP"] = true |
| 746 |
} |
| 747 |
ctx.SaneVendor = sanitize(ctx.Vendor) |
| 748 |
for { |
| 749 |
last := ctx.SaneVendor |
| 750 |
for _, suf := range []string{"_inc", "_co", "_corp"} { |
| 751 |
ctx.SaneVendor = strings.TrimSuffix(ctx.SaneVendor, suf) |
| 752 |
} |
| 753 |
if last == ctx.SaneVendor { |
| 754 |
break |
| 755 |
} |
| 756 |
} |
| 757 |
ctx.MoboID = ctx.SaneVendor + "/" + sanitize(ctx.Model) |
| 758 |
ctx.KconfigName = "BOARD_" + strings.ToUpper(ctx.SaneVendor+"_"+sanitize(ctx.Model)) |
| 759 |
ctx.BaseDirectory = *FlagOutDir + MoboDir + ctx.MoboID |
| 760 |
KconfigString["MAINBOARD_DIR"] = ctx.MoboID |
| 761 |
KconfigString["MAINBOARD_PART_NUMBER"] = ctx.Model |
| 762 |
|
| 763 |
os.MkdirAll(ctx.BaseDirectory, 0700) |
| 764 |
|
| 765 |
makeVendor(ctx) |
| 766 |
|
| 767 |
ScanRoot(ctx) |
| 768 |
|
| 769 |
if IGDEnabled { |
| 770 |
KconfigBool["MAINBOARD_HAS_LIBGFXINIT"] = true |
| 771 |
KconfigComment["MAINBOARD_HAS_LIBGFXINIT"] = "FIXME: check this" |
| 772 |
AddRAMStageFile("gma-mainboard.ads", "CONFIG_MAINBOARD_USE_LIBGFXINIT") |
| 773 |
} |
| 774 |
|
| 775 |
if len(BootBlockFiles) > 0 || len(ROMStageFiles) > 0 || len(RAMStageFiles) > 0 || len(SMMFiles) > 0 { |
| 776 |
mf := Create(ctx, "Makefile.mk") |
| 777 |
defer mf.Close() |
| 778 |
Add_SPDX(mf, Makefile, GPL2_only) |
| 779 |
writeMF(mf, BootBlockFiles, "bootblock") |
| 780 |
writeMF(mf, ROMStageFiles, "romstage") |
| 781 |
writeMF(mf, RAMStageFiles, "ramstage") |
| 782 |
writeMF(mf, SMMFiles, "smm") |
| 783 |
} |
| 784 |
|
| 785 |
devtree := Create(ctx, "devicetree.cb") |
| 786 |
defer devtree.Close() |
| 787 |
|
| 788 |
MatchDev(&DevTree) |
| 789 |
WriteDev(devtree, 0, "", DevTree) |
| 790 |
|
| 791 |
if MainboardInit != "" || MainboardEnable != "" || MainboardIncludes != nil { |
| 792 |
mainboard := Create(ctx, "mainboard.c") |
| 793 |
defer mainboard.Close() |
| 794 |
Add_SPDX(mainboard, C, GPL2_only) |
| 795 |
mainboard.WriteString("#include <device/device.h>\n") |
| 796 |
for _, include := range MainboardIncludes { |
| 797 |
mainboard.WriteString("#include <" + include + ">\n") |
| 798 |
} |
| 799 |
mainboard.WriteString("\n") |
| 800 |
if MainboardInit != "" { |
| 801 |
mainboard.WriteString(`static void mainboard_init(struct device *dev) |
| 802 |
{ |
| 803 |
` + MainboardInit + "}\n\n") |
| 804 |
} |
| 805 |
if MainboardInit != "" || MainboardEnable != "" { |
| 806 |
mainboard.WriteString("static void mainboard_enable(struct device *dev)\n{\n") |
| 807 |
if MainboardInit != "" { |
| 808 |
mainboard.WriteString("\tdev->ops->init = mainboard_init;\n\n") |
| 809 |
} |
| 810 |
mainboard.WriteString(MainboardEnable) |
| 811 |
mainboard.WriteString("}\n\n") |
| 812 |
mainboard.WriteString(`struct chip_operations mainboard_ops = { |
| 813 |
.enable_dev = mainboard_enable, |
| 814 |
}; |
| 815 |
`) |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
bi := Create(ctx, "board_info.txt") |
| 820 |
defer bi.Close() |
| 821 |
|
| 822 |
fixme := "" |
| 823 |
|
| 824 |
if dmi.IsLaptop { |
| 825 |
bi.WriteString("Category: laptop\n") |
| 826 |
} else { |
| 827 |
bi.WriteString("Category: desktop\n") |
| 828 |
fixme += "check category, " |
| 829 |
} |
| 830 |
|
| 831 |
missing := "ROM package, ROM socketed" |
| 832 |
|
| 833 |
if ROMProtocol != "" { |
| 834 |
fmt.Fprintf(bi, "ROM protocol: %s\n", ROMProtocol) |
| 835 |
} else { |
| 836 |
missing += ", ROM protocol" |
| 837 |
} |
| 838 |
|
| 839 |
if FlashROMSupport != "" { |
| 840 |
fmt.Fprintf(bi, "Flashrom support: %s\n", FlashROMSupport) |
| 841 |
} else { |
| 842 |
missing += ", Flashrom support" |
| 843 |
} |
| 844 |
|
| 845 |
missing += ", Release year" |
| 846 |
|
| 847 |
if fixme != "" { |
| 848 |
fmt.Fprintf(bi, "FIXME: %s, put %s\n", fixme, missing) |
| 849 |
} else { |
| 850 |
fmt.Fprintf(bi, "FIXME: put %s\n", missing) |
| 851 |
} |
| 852 |
|
| 853 |
if ROMSizeKB == 0 { |
| 854 |
KconfigBool["BOARD_ROMSIZE_KB_2048"] = true |
| 855 |
KconfigComment["BOARD_ROMSIZE_KB_2048"] = "FIXME: correct this" |
| 856 |
} else { |
| 857 |
KconfigBool[fmt.Sprintf("BOARD_ROMSIZE_KB_%d", ROMSizeKB)] = true |
| 858 |
} |
| 859 |
|
| 860 |
makeKconfig(ctx) |
| 861 |
makeKconfigName(ctx) |
| 862 |
|
| 863 |
dsdt := Create(ctx, "dsdt.asl") |
| 864 |
defer dsdt.Close() |
| 865 |
Add_SPDX(dsdt, ASL, GPL2_only) |
| 866 |
|
| 867 |
for _, define := range DSDTDefines { |
| 868 |
if define.Comment != "" { |
| 869 |
fmt.Fprintf(dsdt, "\t/* %s. */\n", define.Comment) |
| 870 |
} |
| 871 |
dsdt.WriteString("#define " + define.Key + " " + define.Value + "\n") |
| 872 |
} |
| 873 |
|
| 874 |
dsdt.WriteString( |
| 875 |
`#include <acpi/acpi.h> |
| 876 |
|
| 877 |
DefinitionBlock( |
| 878 |
"dsdt.aml", |
| 879 |
"DSDT", |
| 880 |
ACPI_DSDT_REV_2, |
| 881 |
OEM_ID, |
| 882 |
ACPI_TABLE_CREATOR, |
| 883 |
0x20141018 |
| 884 |
) |
| 885 |
{ |
| 886 |
#include <acpi/dsdt_top.asl> |
| 887 |
#include "acpi/platform.asl" |
| 888 |
`) |
| 889 |
|
| 890 |
for _, x := range DSDTIncludes { |
| 891 |
if x.Comment != "" { |
| 892 |
fmt.Fprintf(dsdt, "\t/* %s. */\n", x.Comment) |
| 893 |
} |
| 894 |
fmt.Fprintf(dsdt, "\t#include <%s>\n", x.File) |
| 895 |
} |
| 896 |
|
| 897 |
dsdt.WriteString(` |
| 898 |
Device (\_SB.PCI0) |
| 899 |
{ |
| 900 |
`) |
| 901 |
for _, x := range DSDTPCI0Includes { |
| 902 |
if x.Comment != "" { |
| 903 |
fmt.Fprintf(dsdt, "\t/* %s. */\n", x.Comment) |
| 904 |
} |
| 905 |
fmt.Fprintf(dsdt, "\t\t#include <%s>\n", x.File) |
| 906 |
} |
| 907 |
dsdt.WriteString( |
| 908 |
` } |
| 909 |
} |
| 910 |
`) |
| 911 |
|
| 912 |
if IGDEnabled { |
| 913 |
gma := Create(ctx, "gma-mainboard.ads") |
| 914 |
defer gma.Close() |
| 915 |
Add_SPDX(gma, Ada, GPL2_or_later) |
| 916 |
|
| 917 |
gma.WriteString(`with HW.GFX.GMA; |
| 918 |
with HW.GFX.GMA.Display_Probing; |
| 919 |
|
| 920 |
use HW.GFX.GMA; |
| 921 |
use HW.GFX.GMA.Display_Probing; |
| 922 |
|
| 923 |
private package GMA.Mainboard is |
| 924 |
|
| 925 |
-- FIXME: check this |
| 926 |
ports : constant Port_List := |
| 927 |
(DP1, |
| 928 |
DP2, |
| 929 |
DP3, |
| 930 |
HDMI1, |
| 931 |
HDMI2, |
| 932 |
HDMI3, |
| 933 |
Analog, |
| 934 |
LVDS, |
| 935 |
eDP); |
| 936 |
|
| 937 |
end GMA.Mainboard; |
| 938 |
`) |
| 939 |
} |
| 940 |
outputPath, _ := filepath.Abs(ctx.BaseDirectory) |
| 941 |
fmt.Printf("Done! Generated sources are in %s\n", outputPath) |
| 942 |
} |