1 /* This is just an experiment. Full automatic porting
2 is probably not possible but a lot can be automated. */
22 type PCIDevData
struct {
29 type PCIDevice
interface {
30 Scan(ctx Context
, addr PCIDevData
)
33 type InteltoolData
struct {
34 GPIO
map[uint16]uint32
35 RCBA
map[uint16]uint32
46 type AzaliaCodec
struct {
51 PinConfig
map[int]uint32
54 type DevReader
interface {
55 GetPCIList() []PCIDevData
57 GetInteltool() InteltoolData
58 GetAzaliaCodecs() []AzaliaCodec
59 GetACPI() map[string][]byte
60 GetCPUModel() []uint32
62 GetIOPorts() []IOPorts
72 type SouthBridger
interface {
73 GetGPIOHeader() string
77 NeedRouteGPIOManually()
80 var SouthBridge SouthBridger
81 var BootBlockFiles
map[string]string = map[string]string{}
82 var ROMStageFiles
map[string]string = map[string]string{}
83 var RAMStageFiles
map[string]string = map[string]string{}
84 var SMMFiles
map[string]string = map[string]string{}
85 var MainboardInit
string
86 var MainboardEnable
string
87 var MainboardIncludes
[]string
99 var KconfigBool
map[string]bool = map[string]bool{}
100 var KconfigComment
map[string]string = map[string]string{}
101 var KconfigString
map[string]string = map[string]string{}
102 var KconfigHex
map[string]uint32 = map[string]uint32{}
103 var KconfigInt
map[string]int = map[string]int{}
106 var FlashROMSupport
= ""
108 func GetLE16(inp
[]byte) uint16 {
109 return uint16(inp
[0]) |
(uint16(inp
[1]) << 8)
112 func FormatHexLE16(inp
[]byte) string {
113 return fmt
.Sprintf("0x%04x", GetLE16(inp
))
116 func FormatHex32(u
uint32) string {
117 return fmt
.Sprintf("0x%08x", u
)
120 func FormatHex8(u
uint8) string {
121 return fmt
.Sprintf("0x%02x", u
)
124 func FormatInt32(u
uint32) string {
125 return fmt
.Sprintf("%d", u
)
128 func FormatHexLE32(d
[]uint8) string {
129 u
:= uint32(d
[0]) |
(uint32(d
[1]) << 8) |
(uint32(d
[2]) << 16) |
(uint32(d
[3]) << 24)
130 return FormatHex32(u
)
133 func FormatBool(inp
bool) string {
141 func sanitize(inp
string) string {
142 result
:= strings
.ToLower(inp
)
143 result
= strings
.Replace(result
, " ", "_", -1)
144 result
= strings
.Replace(result
, ",", "_", -1)
145 result
= strings
.Replace(result
, "-", "_", -1)
146 for strings
.HasSuffix(result
, ".") {
147 result
= result
[0 : len(result
)-1]
152 func AddBootBlockFile(Name
string, Condition
string) {
153 BootBlockFiles
[Name
] = Condition
156 func AddROMStageFile(Name
string, Condition
string) {
157 ROMStageFiles
[Name
] = Condition
160 func AddRAMStageFile(Name
string, Condition
string) {
161 RAMStageFiles
[Name
] = Condition
164 func AddSMMFile(Name
string, Condition
string) {
165 SMMFiles
[Name
] = Condition
168 func IsIOPortUsedBy(ctx Context
, port
uint16, name
string) bool {
169 for _
, io
:= range ctx
.InfoSource
.GetIOPorts() {
170 if io
.Start
<= port
&& port
<= io
.End
&& io
.Usage
== name
{
177 var FlagOutDir
= flag
.String("coreboot_dir", ".", "Resulting coreboot directory")
179 func writeMF(mf
*os
.File
, files
map[string]string, category
string) {
181 for file
, _
:= range files
{
182 keys
= append(keys
, file
)
187 for _
, file
:= range keys
{
188 condition
:= files
[file
]
190 fmt
.Fprintf(mf
, "%s-y += %s\n", category
, file
)
192 fmt
.Fprintf(mf
, "%s-$(%s) += %s\n", category
, condition
, file
)
197 func Create(ctx Context
, name
string) *os
.File
{
198 li
:= strings
.LastIndex(name
, "/")
200 os
.MkdirAll(ctx
.BaseDirectory
+"/"+name
[0:li
], 0700)
202 mf
, err
:= os
.Create(ctx
.BaseDirectory
+ "/" + name
)
209 func Add_gpl(f
*os
.File
) {
210 fmt
.Fprintln(f
, "/* SPDX-License-Identifier: GPL-2.0-only */")
214 func RestorePCI16Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
215 fmt
.Fprintf(f
, " pci_write_config16(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x);\n",
216 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
217 pcidev
.ConfigDump
[addr
+1],
218 pcidev
.ConfigDump
[addr
])
221 func RestorePCI32Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
222 fmt
.Fprintf(f
, " pci_write_config32(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x%02x%02x);\n",
223 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
224 pcidev
.ConfigDump
[addr
+3],
225 pcidev
.ConfigDump
[addr
+2],
226 pcidev
.ConfigDump
[addr
+1],
227 pcidev
.ConfigDump
[addr
])
230 func RestoreRCBA32(f
*os
.File
, inteltool InteltoolData
, addr
uint16) {
231 fmt
.Fprintf(f
, "\tRCBA32(0x%04x) = 0x%08x;\n", addr
, inteltool
.RCBA
[addr
])
234 type PCISlot
struct {
237 additionalComment
string
241 type DevTreeNode
struct {
246 Registers
map[string]string
247 IOs
map[uint16]uint16
248 Children
[]DevTreeNode
259 var DevTree DevTreeNode
260 var MissingChildren
map[string][]DevTreeNode
= map[string][]DevTreeNode
{}
261 var unmatchedPCIChips
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
262 var unmatchedPCIDevices
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
264 func Offset(dt
*os
.File
, offset
int) {
265 for i
:= 0; i
< offset
; i
++ {
266 fmt
.Fprintf(dt
, "\t")
270 func MatchDev(dev
*DevTreeNode
) {
271 for idx
:= range dev
.Children
{
272 MatchDev(&dev
.Children
[idx
])
275 for _
, slot
:= range dev
.PCISlots
{
276 slotChip
, ok
:= unmatchedPCIChips
[slot
.PCIAddr
]
282 if slot
.additionalComment
!= "" && slotChip
.Comment
!= "" {
283 slotChip
.Comment
= slot
.additionalComment
+ " " + slotChip
.Comment
285 slotChip
.Comment
= slot
.additionalComment
+ slotChip
.Comment
288 delete(unmatchedPCIChips
, slot
.PCIAddr
)
290 dev
.Children
= append(dev
.Children
, slotChip
)
293 if dev
.PCIController
{
294 for slot
, slotDev
:= range unmatchedPCIChips
{
295 if slot
.Bus
== dev
.ChildPCIBus
{
296 delete(unmatchedPCIChips
, slot
)
298 dev
.Children
= append(dev
.Children
, slotDev
)
303 for _
, slot
:= range dev
.PCISlots
{
304 slotDev
, ok
:= unmatchedPCIDevices
[slot
.PCIAddr
]
307 dev
.Children
= append(dev
.Children
,
309 Registers
: map[string]string{},
314 Comment
: slot
.additionalComment
,
322 if slot
.additionalComment
!= "" && slotDev
.Comment
!= "" {
323 slotDev
.Comment
= slot
.additionalComment
+ " " + slotDev
.Comment
325 slotDev
.Comment
= slot
.additionalComment
+ slotDev
.Comment
329 dev
.Children
= append(dev
.Children
, slotDev
)
330 delete(unmatchedPCIDevices
, slot
.PCIAddr
)
333 if dev
.MissingParent
!= "" {
334 for _
, child
:= range MissingChildren
[dev
.MissingParent
] {
336 dev
.Children
= append(dev
.Children
, child
)
338 delete(MissingChildren
, dev
.MissingParent
)
341 if dev
.PCIController
{
342 for slot
, slotDev
:= range unmatchedPCIDevices
{
343 if slot
.Bus
== dev
.ChildPCIBus
{
345 dev
.Children
= append(dev
.Children
, slotDev
)
346 delete(unmatchedPCIDevices
, slot
)
352 func writeOn(dt
*os
.File
, dev DevTreeNode
) {
354 fmt
.Fprintf(dt
, "off")
356 fmt
.Fprintf(dt
, "on")
360 func WriteDev(dt
*os
.File
, offset
int, alias
string, dev DevTreeNode
) {
363 case "cpu_cluster", "domain":
364 fmt
.Fprintf(dt
, "device %s 0x%x ", dev
.Chip
, dev
.Dev
)
368 fmt
.Fprintf(dt
, "device ref %s ", alias
)
370 fmt
.Fprintf(dt
, "device %s %02x.%x ", dev
.Chip
, dev
.Dev
, dev
.Func
)
374 fmt
.Fprintf(dt
, "device %s %02x ", dev
.Chip
, dev
.Dev
)
377 fmt
.Fprintf(dt
, "chip %s", dev
.Chip
)
379 if dev
.Comment
!= "" {
380 fmt
.Fprintf(dt
, " # %s", dev
.Comment
)
382 fmt
.Fprintf(dt
, "\n")
383 if dev
.Chip
== "pci" && dev
.SubSystem
!= 0 && dev
.SubVendor
!= 0 {
385 fmt
.Fprintf(dt
, "subsystemid 0x%04x 0x%04x\n", dev
.SubVendor
, dev
.SubSystem
)
389 for reg
, _
:= range dev
.Registers
{
390 keys
= append(keys
, reg
)
395 for _
, reg
:= range keys
{
396 val
:= dev
.Registers
[reg
]
398 fmt
.Fprintf(dt
, "register \"%s\" = \"%s\"\n", reg
, val
)
402 for reg
, _
:= range dev
.IOs
{
403 ios
= append(ios
, int(reg
))
408 for _
, reg
:= range ios
{
409 val
:= dev
.IOs
[uint16(reg
)]
411 fmt
.Fprintf(dt
, "io 0x%x = 0x%x\n", reg
, val
)
414 for _
, child
:= range dev
.Children
{
416 for _
, slot
:= range dev
.PCISlots
{
417 if slot
.PCIAddr
.Bus
== child
.Bus
&&
418 slot
.PCIAddr
.Dev
== child
.Dev
&& slot
.PCIAddr
.Func
== child
.Func
{
422 WriteDev(dt
, offset
+1, alias
, child
)
426 fmt
.Fprintf(dt
, "end\n")
429 func PutChip(domain
string, cur DevTreeNode
) {
430 MissingChildren
[domain
] = append(MissingChildren
[domain
], cur
)
433 func PutPCIChip(addr PCIDevData
, cur DevTreeNode
) {
434 unmatchedPCIChips
[addr
.PCIAddr
] = cur
437 func PutPCIDevParent(addr PCIDevData
, comment
string, parent
string) {
439 Registers
: map[string]string{},
444 MissingParent
: parent
,
447 if addr
.ConfigDump
[0xa] == 0x04 && addr
.ConfigDump
[0xb] == 0x06 {
448 cur
.PCIController
= true
449 cur
.ChildPCIBus
= int(addr
.ConfigDump
[0x19])
452 for capPtr
:= addr
.ConfigDump
[0x34]; capPtr
!= 0; capPtr
= addr
.ConfigDump
[capPtr
+1] {
453 /* Avoid hangs. There are only 0x100 different possible values for capPtr.
454 If we iterate longer than that, we're in endless loop. */
459 if addr
.ConfigDump
[capPtr
] == 0x0d {
460 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[capPtr
+4 : capPtr
+6])
461 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[capPtr
+6 : capPtr
+8])
465 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[0x2c:0x2e])
466 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[0x2e:0x30])
468 unmatchedPCIDevices
[addr
.PCIAddr
] = cur
471 func PutPCIDev(addr PCIDevData
, comment
string) {
472 PutPCIDevParent(addr
, comment
, "")
475 type GenericPCI
struct {
481 type GenericVGA
struct {
485 type DSDTInclude
struct {
490 type DSDTDefine
struct {
496 var DSDTIncludes
[]DSDTInclude
497 var DSDTPCI0Includes
[]DSDTInclude
498 var DSDTDefines
[]DSDTDefine
500 func (g GenericPCI
) Scan(ctx Context
, addr PCIDevData
) {
501 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
504 var IGDEnabled
bool = false
506 func (g GenericVGA
) Scan(ctx Context
, addr PCIDevData
) {
507 KconfigString
["VGA_BIOS_ID"] = fmt
.Sprintf("%04x,%04x",
510 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
514 func makeKconfigName(ctx Context
) {
515 kn
:= Create(ctx
, "Kconfig.name")
518 fmt
.Fprintf(kn
, "config %s\n\tbool \"%s\"\n", ctx
.KconfigName
, ctx
.Model
)
521 func makeComment(name
string) string {
522 cmt
, ok
:= KconfigComment
[name
]
529 func makeKconfig(ctx Context
) {
530 kc
:= Create(ctx
, "Kconfig")
533 fmt
.Fprintf(kc
, "if %s\n\n", ctx
.KconfigName
)
535 fmt
.Fprintf(kc
, "config BOARD_SPECIFIC_OPTIONS\n\tdef_bool y\n")
537 for name
, val
:= range KconfigBool
{
539 keys
= append(keys
, name
)
545 for _
, name
:= range keys
{
546 fmt
.Fprintf(kc
, "\tselect %s%s\n", name
, makeComment(name
))
550 for name
, val
:= range KconfigBool
{
552 keys
= append(keys
, name
)
558 for _
, name
:= range keys
{
563 `, name
, makeComment(name
))
567 for name
, _
:= range KconfigString
{
568 keys
= append(keys
, name
)
573 for _
, name
:= range keys
{
578 `, name
, makeComment(name
), KconfigString
[name
])
582 for name
, _
:= range KconfigHex
{
583 keys
= append(keys
, name
)
588 for _
, name
:= range keys
{
593 `, name
, makeComment(name
), KconfigHex
[name
])
597 for name
, _
:= range KconfigInt
{
598 keys
= append(keys
, name
)
603 for _
, name
:= range keys
{
608 `, name
, makeComment(name
), KconfigInt
[name
])
611 fmt
.Fprintf(kc
, "endif\n")
614 const MoboDir
= "/src/mainboard/"
616 func makeVendor(ctx Context
) {
618 vendorSane
:= ctx
.SaneVendor
619 vendorDir
:= *FlagOutDir
+ MoboDir
+ vendorSane
620 vendorUpper
:= strings
.ToUpper(vendorSane
)
621 kconfig
:= vendorDir
+ "/Kconfig"
622 if _
, err
:= os
.Stat(kconfig
); os
.IsNotExist(err
) {
623 f
, err
:= os
.Create(kconfig
)
628 f
.WriteString(`if VENDOR_` + vendorUpper
+ `
631 prompt "Mainboard model"
633 source "src/mainboard/` + vendorSane
+ `/*/Kconfig.name"
637 source "src/mainboard/` + vendorSane
+ `/*/Kconfig"
639 config MAINBOARD_VENDOR
641 default "` + vendor
+ `"
643 endif # VENDOR_` + vendorUpper
+ "\n")
645 kconfigName
:= vendorDir
+ "/Kconfig.name"
646 if _
, err
:= os
.Stat(kconfigName
); os
.IsNotExist(err
) {
647 f
, err
:= os
.Create(kconfigName
)
652 f
.WriteString(`config VENDOR_` + vendorUpper
+ `
653 bool "` + vendor
+ `"
659 func GuessECGPE(ctx Context
) int {
660 /* FIXME:XX Use iasl -d and/or better parsing */
661 dsdt
:= ctx
.InfoSource
.GetACPI()["DSDT"]
662 idx
:= bytes
.Index(dsdt
, []byte{0x08, '_', 'G', 'P', 'E', 0x0a}) /* Name (_GPE, byte). */
664 return int(dsdt
[idx
+6])
669 func GuessSPDMap(ctx Context
) []uint8 {
670 dmi
:= ctx
.InfoSource
.GetDMI()
672 if dmi
.Vendor
== "LENOVO" {
673 return []uint8{0x50, 0x52, 0x51, 0x53}
675 return []uint8{0x50, 0x51, 0x52, 0x53}
683 ctx
.InfoSource
= MakeLogReader()
685 dmi
:= ctx
.InfoSource
.GetDMI()
687 ctx
.Vendor
= dmi
.Vendor
689 if dmi
.Vendor
== "LENOVO" {
690 ctx
.Model
= dmi
.Version
692 ctx
.Model
= dmi
.Model
696 KconfigBool
["SYSTEM_TYPE_LAPTOP"] = true
698 ctx
.SaneVendor
= sanitize(ctx
.Vendor
)
700 last
:= ctx
.SaneVendor
701 for _
, suf
:= range []string{"_inc", "_co", "_corp"} {
702 ctx
.SaneVendor
= strings
.TrimSuffix(ctx
.SaneVendor
, suf
)
704 if last
== ctx
.SaneVendor
{
708 ctx
.MoboID
= ctx
.SaneVendor
+ "/" + sanitize(ctx
.Model
)
709 ctx
.KconfigName
= "BOARD_" + strings
.ToUpper(ctx
.SaneVendor
+"_"+sanitize(ctx
.Model
))
710 ctx
.BaseDirectory
= *FlagOutDir
+ MoboDir
+ ctx
.MoboID
711 KconfigString
["MAINBOARD_DIR"] = ctx
.MoboID
712 KconfigString
["MAINBOARD_PART_NUMBER"] = ctx
.Model
714 os
.MkdirAll(ctx
.BaseDirectory
, 0700)
721 KconfigBool
["MAINBOARD_HAS_LIBGFXINIT"] = true
722 KconfigComment
["MAINBOARD_HAS_LIBGFXINIT"] = "FIXME: check this"
723 AddRAMStageFile("gma-mainboard.ads", "CONFIG_MAINBOARD_USE_LIBGFXINIT")
726 if len(BootBlockFiles
) > 0 ||
len(ROMStageFiles
) > 0 ||
len(RAMStageFiles
) > 0 ||
len(SMMFiles
) > 0 {
727 mf
:= Create(ctx
, "Makefile.mk")
729 writeMF(mf
, BootBlockFiles
, "bootblock")
730 writeMF(mf
, ROMStageFiles
, "romstage")
731 writeMF(mf
, RAMStageFiles
, "ramstage")
732 writeMF(mf
, SMMFiles
, "smm")
735 devtree
:= Create(ctx
, "devicetree.cb")
736 defer devtree
.Close()
739 WriteDev(devtree
, 0, "", DevTree
)
741 if MainboardInit
!= "" || MainboardEnable
!= "" || MainboardIncludes
!= nil {
742 mainboard
:= Create(ctx
, "mainboard.c")
743 defer mainboard
.Close()
745 mainboard
.WriteString("#include <device/device.h>\n")
746 for _
, include
:= range MainboardIncludes
{
747 mainboard
.WriteString("#include <" + include
+ ">\n")
749 mainboard
.WriteString("\n")
750 if MainboardInit
!= "" {
751 mainboard
.WriteString(`static void mainboard_init(struct device *dev)
753 ` + MainboardInit
+ "}\n\n")
755 if MainboardInit
!= "" || MainboardEnable
!= "" {
756 mainboard
.WriteString("static void mainboard_enable(struct device *dev)\n{\n")
757 if MainboardInit
!= "" {
758 mainboard
.WriteString("\tdev->ops->init = mainboard_init;\n\n")
760 mainboard
.WriteString(MainboardEnable
)
761 mainboard
.WriteString("}\n\n")
762 mainboard
.WriteString(`struct chip_operations mainboard_ops = {
763 .enable_dev = mainboard_enable,
769 bi
:= Create(ctx
, "board_info.txt")
775 bi
.WriteString("Category: laptop\n")
777 bi
.WriteString("Category: desktop\n")
778 fixme
+= "check category, "
781 missing
:= "ROM package, ROM socketed"
783 if ROMProtocol
!= "" {
784 fmt
.Fprintf(bi
, "ROM protocol: %s\n", ROMProtocol
)
786 missing
+= ", ROM protocol"
789 if FlashROMSupport
!= "" {
790 fmt
.Fprintf(bi
, "Flashrom support: %s\n", FlashROMSupport
)
792 missing
+= ", Flashrom support"
795 missing
+= ", Release year"
798 fmt
.Fprintf(bi
, "FIXME: %s, put %s\n", fixme
, missing
)
800 fmt
.Fprintf(bi
, "FIXME: put %s\n", missing
)
804 KconfigBool
["BOARD_ROMSIZE_KB_2048"] = true
805 KconfigComment
["BOARD_ROMSIZE_KB_2048"] = "FIXME: correct this"
807 KconfigBool
[fmt
.Sprintf("BOARD_ROMSIZE_KB_%d", ROMSizeKB
)] = true
813 dsdt
:= Create(ctx
, "dsdt.asl")
817 for _
, define
:= range DSDTDefines
{
818 if define
.Comment
!= "" {
819 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", define
.Comment
)
821 dsdt
.WriteString("#define " + define
.Key
+ " " + define
.Value
+ "\n")
825 `#include <acpi/acpi.h>
836 #include <acpi/dsdt_top.asl>
837 #include "acpi/platform.asl"
840 for _
, x
:= range DSDTIncludes
{
842 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
844 fmt
.Fprintf(dsdt
, "\t#include <%s>\n", x
.File
)
851 for _
, x
:= range DSDTPCI0Includes
{
853 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
855 fmt
.Fprintf(dsdt
, "\t\t#include <%s>\n", x
.File
)
863 gma
:= Create(ctx
, "gma-mainboard.ads")
866 gma
.WriteString(`-- SPDX-License-Identifier: GPL-2.0-or-later
869 with HW.GFX.GMA.Display_Probing;
872 use HW.GFX.GMA.Display_Probing;
874 private package GMA.Mainboard is
877 ports : constant Port_List :=
891 outputPath
, _
:= filepath
.Abs(ctx
.BaseDirectory
)
892 fmt
.Printf("Done! Generated sources are in %s\n", outputPath
)