From 2a2c78aeeaa16e8847907a68d21992e083c47fb7 Mon Sep 17 00:00:00 2001
From: Nicholas Chin <nic.c3.14@gmail.com>
Date: Sun, 23 Jun 2024 10:15:47 -0600
Subject: util/autoport: Make printing of SPDX headers generic

Previously, Add_gpl() was only used with C and ASL source code files,
and was hard coded to use the C /* */ style comment, preventing it from
being used with files with other comment styles. Convert this into a
generic function for adding arbitrary SPDX license identifiers for
arbitrary filetypes. This replaces the hard coded GPL-2.0-or-later
string used in gma-mainboard.ads with a call to the new function.

This is also used to add SPDX headers to Kconfig and Makefile sources;
as previous commits added them to all such files in the tree.

Tested against logs from a Latitude E6430 (Ivy Bridge) and Precision
M6800 (Haswell) to check that license headers that were already being
generated did not change.

Change-Id: I24a1ccd0afb7045e878bf6eaae7a23f828a9240d
Signed-off-by: Nicholas Chin <nic.c3.14@gmail.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/83184
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
---
 util/autoport/azalia.go            |  2 +-
 util/autoport/bd82x6x.go           |  4 ++--
 util/autoport/ec_fixme.go          |  6 ++---
 util/autoport/ec_lenovo.go         |  8 +++----
 util/autoport/ec_none.go           |  2 +-
 util/autoport/gpio_common.go       |  2 +-
 util/autoport/lynxpoint.go         |  4 ++--
 util/autoport/lynxpoint_lp_gpio.go |  2 +-
 util/autoport/main.go              | 47 ++++++++++++++++++++++++++++++++------
 9 files changed, 55 insertions(+), 22 deletions(-)

(limited to 'util/autoport')

diff --git a/util/autoport/azalia.go b/util/autoport/azalia.go
index 6dd78b1e62..1540398281 100644
--- a/util/autoport/azalia.go
+++ b/util/autoport/azalia.go
@@ -12,7 +12,7 @@ func (i azalia) Scan(ctx Context, addr PCIDevData) {
 	az := Create(ctx, "hda_verb.c")
 	defer az.Close()
 
-	Add_gpl(az)
+	Add_SPDX(az, C, GPL2_only)
 	az.WriteString(
 		`#include <device/azalia_device.h>
 
diff --git a/util/autoport/bd82x6x.go b/util/autoport/bd82x6x.go
index 2dc4961151..71b97d430d 100644
--- a/util/autoport/bd82x6x.go
+++ b/util/autoport/bd82x6x.go
@@ -181,7 +181,7 @@ func (b bd82x6x) Scan(ctx Context, addr PCIDevData) {
 
 	sb := Create(ctx, "early_init.c")
 	defer sb.Close()
-	Add_gpl(sb)
+	Add_SPDX(sb, C, GPL2_only)
 
 	sb.WriteString(`#include <bootblock_common.h>
 #include <device/pci_ops.h>
@@ -246,7 +246,7 @@ void bootblock_mainboard_early_init(void)
 	gnvs := Create(ctx, "acpi_tables.c")
 	defer gnvs.Close()
 
-	Add_gpl(gnvs)
+	Add_SPDX(gnvs, C, GPL2_only)
 	gnvs.WriteString(`#include <acpi/acpi_gnvs.h>
 #include <soc/nvs.h>
 
diff --git a/util/autoport/ec_fixme.go b/util/autoport/ec_fixme.go
index 54f78ab432..e0a6554016 100644
--- a/util/autoport/ec_fixme.go
+++ b/util/autoport/ec_fixme.go
@@ -19,7 +19,7 @@ func FIXMEEC(ctx Context) {
 		SouthBridge.EnableGPE(SouthBridge.DecodeGPE(sbGPE))
 	}
 
-	Add_gpl(ap)
+	Add_SPDX(ap, ASL, GPL2_only)
 	ap.WriteString(
 		`Method(_WAK, 1)
 {
@@ -65,7 +65,7 @@ Method(_PTS,1)
 	defer si.Close()
 
 	if hasKeyboard {
-		Add_gpl(si)
+		Add_SPDX(si, ASL, GPL2_only)
 		si.WriteString("#include <drivers/pc80/pc/ps2_controller.asl>\n")
 		MainboardInit += fmt.Sprintf("\tpc_keyboard_init(NO_AUX_DEVICE);\n")
 		MainboardIncludes = append(MainboardIncludes, "pc80/keyboard.h")
@@ -74,7 +74,7 @@ Method(_PTS,1)
 	ec := Create(ctx, "acpi/ec.asl")
 	defer ec.Close()
 
-	Add_gpl(ec)
+	Add_SPDX(ec, ASL, GPL2_only)
 	ec.WriteString(`Device(EC)
 {
 	Name (_HID, EISAID("PNP0C09"))
diff --git a/util/autoport/ec_lenovo.go b/util/autoport/ec_lenovo.go
index a34960ff68..38257d0eda 100644
--- a/util/autoport/ec_lenovo.go
+++ b/util/autoport/ec_lenovo.go
@@ -39,7 +39,7 @@ func LenovoEC(ctx Context) {
 			Value: "1",
 		}, GPEDefine)
 
-	Add_gpl(ap)
+	Add_SPDX(ap, ASL, GPL2_only)
 	ap.WriteString(
 		`Method(_WAK, 1)
 {
@@ -58,7 +58,7 @@ Method(_PTS,1)
 	si := Create(ctx, "acpi/superio.asl")
 	defer si.Close()
 
-	Add_gpl(si)
+	Add_SPDX(si, ASL, GPL2_only)
 	si.WriteString("#include <drivers/pc80/pc/ps2_controller.asl>\n")
 
 	/* FIXME:XX Move this to ec/lenovo.  */
@@ -67,7 +67,7 @@ Method(_PTS,1)
 
 	AddSMMFile("smihandler.c", "")
 
-	Add_gpl(smi)
+	Add_SPDX(smi, C, GPL2_only)
 	smi.WriteString(
 		`#include <arch/io.h>
 #include <console/console.h>
@@ -146,7 +146,7 @@ void mainboard_smi_sleep(u8 slp_typ)
 	ec := Create(ctx, "acpi/ec.asl")
 	defer ec.Close()
 
-	Add_gpl(ec)
+	Add_SPDX(ec, ASL, GPL2_only)
 	ec.WriteString("#include <ec/lenovo/h8/acpi/ec.asl>\n")
 
 	KconfigBool["EC_LENOVO_PMH7"] = true
diff --git a/util/autoport/ec_none.go b/util/autoport/ec_none.go
index bcb61bf03f..a1a0df94f0 100644
--- a/util/autoport/ec_none.go
+++ b/util/autoport/ec_none.go
@@ -4,7 +4,7 @@ func NoEC(ctx Context) {
 	ap := Create(ctx, "acpi/platform.asl")
 	defer ap.Close()
 
-	Add_gpl(ap)
+	Add_SPDX(ap, ASL, GPL2_only)
 	ap.WriteString(
 		`Method(_WAK, 1)
 {
diff --git a/util/autoport/gpio_common.go b/util/autoport/gpio_common.go
index a869dce464..36e1ed0a02 100644
--- a/util/autoport/gpio_common.go
+++ b/util/autoport/gpio_common.go
@@ -41,7 +41,7 @@ func GPIO(ctx Context, inteltool InteltoolData) {
 	AddBootBlockFile("gpio.c", "")
 	AddROMStageFile("gpio.c", "")
 
-	Add_gpl(gpio)
+	Add_SPDX(gpio, C, GPL2_only)
 	gpio.WriteString("#include <southbridge/intel/common/gpio.h>\n\n")
 
 	addresses := [3][6]int{
diff --git a/util/autoport/lynxpoint.go b/util/autoport/lynxpoint.go
index 7168686cdb..899736ef22 100644
--- a/util/autoport/lynxpoint.go
+++ b/util/autoport/lynxpoint.go
@@ -258,7 +258,7 @@ func (b lynxpoint) Scan(ctx Context, addr PCIDevData) {
 	AddBootBlockFile("bootblock.c", "")
 	bb := Create(ctx, "bootblock.c")
 	defer bb.Close()
-	Add_gpl(bb)
+	Add_SPDX(bb, C, GPL2_only)
 	bb.WriteString(`#include <southbridge/intel/lynxpoint/pch.h>
 
 /* FIXME: remove this if not needed */
@@ -269,7 +269,7 @@ void mainboard_config_superio(void)
 
 	sb := Create(ctx, "romstage.c")
 	defer sb.Close()
-	Add_gpl(sb)
+	Add_SPDX(sb, C, GPL2_only)
 	sb.WriteString(`#include <stdint.h>
 #include <northbridge/intel/haswell/haswell.h>
 #include <southbridge/intel/lynxpoint/pch.h>
diff --git a/util/autoport/lynxpoint_lp_gpio.go b/util/autoport/lynxpoint_lp_gpio.go
index ee5944c91d..7163a8e7d8 100644
--- a/util/autoport/lynxpoint_lp_gpio.go
+++ b/util/autoport/lynxpoint_lp_gpio.go
@@ -230,7 +230,7 @@ func Lynxpoint_LP_GPIO(ctx Context, inteltool InteltoolData) {
 
 	AddROMStageFile("gpio.c", "")
 
-	Add_gpl(gpio)
+	Add_SPDX(gpio, C, GPL2_only)
 	gpio.WriteString(`#include <southbridge/intel/lynxpoint/lp_gpio.h>
 
 const struct pch_lp_gpio_map mainboard_lp_gpio_map[] = {
diff --git a/util/autoport/main.go b/util/autoport/main.go
index 3b085152c5..c4f884a985 100644
--- a/util/autoport/main.go
+++ b/util/autoport/main.go
@@ -99,6 +99,31 @@ type Context struct {
 	SaneVendor    string
 }
 
+type Filetype int
+
+const (
+	Ada Filetype = iota
+	ASL
+	C
+	Kconfig
+	Makefile
+)
+
+var CommentFormatStrings map[Filetype]string = map[Filetype]string {
+	Ada: "-- %s\n",
+	ASL: "/* %s */\n",
+	C: "/* %s */\n",
+	Kconfig: "## %s\n",
+	Makefile: "## %s\n",
+}
+
+type License string
+
+const (
+    GPL2_only License = "GPL-2.0-only"
+    GPL2_or_later     = "GPL-2.0-or-later"
+)
+
 var KconfigBool map[string]bool = map[string]bool{}
 var KconfigComment map[string]string = map[string]string{}
 var KconfigString map[string]string = map[string]string{}
@@ -209,11 +234,15 @@ func Create(ctx Context, name string) *os.File {
 	return mf
 }
 
-func Add_gpl(f *os.File) {
-	fmt.Fprintln(f, "/* SPDX-License-Identifier: GPL-2.0-only */")
+func Add_SPDX(f *os.File, filetype Filetype, license License) {
+	Add_Comment(f, filetype, "SPDX-License-Identifier: " + string(license))
 	fmt.Fprintln(f)
 }
 
+func Add_Comment(f *os.File, filetype Filetype, comment string) {
+	fmt.Fprintf(f, CommentFormatStrings[filetype], comment)
+}
+
 func RestorePCI16Simple(f *os.File, pcidev PCIDevData, addr uint16) {
 	fmt.Fprintf(f, "	pci_write_config16(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x);\n",
 		pcidev.Bus, pcidev.Dev, pcidev.Func, addr,
@@ -522,6 +551,7 @@ func (g GenericVGA) Scan(ctx Context, addr PCIDevData) {
 func makeKconfigName(ctx Context) {
 	kn := Create(ctx, "Kconfig.name")
 	defer kn.Close()
+	Add_SPDX(kn, Kconfig, GPL2_only)
 
 	fmt.Fprintf(kn, "config %s\n\tbool \"%s\"\n", ctx.KconfigName, ctx.Model)
 }
@@ -537,6 +567,7 @@ func makeComment(name string) string {
 func makeKconfig(ctx Context) {
 	kc := Create(ctx, "Kconfig")
 	defer kc.Close()
+	Add_SPDX(kc, Kconfig, GPL2_only)
 
 	fmt.Fprintf(kc, "if %s\n\n", ctx.KconfigName)
 
@@ -633,6 +664,7 @@ func makeVendor(ctx Context) {
 			log.Fatal(err)
 		}
 		defer f.Close()
+		Add_SPDX(f, Kconfig, GPL2_only)
 		f.WriteString(`if VENDOR_` + vendorUpper + `
 
 choice
@@ -657,6 +689,7 @@ endif # VENDOR_` + vendorUpper + "\n")
 			log.Fatal(err)
 		}
 		defer f.Close()
+		Add_SPDX(f, Kconfig, GPL2_only)
 		f.WriteString(`config VENDOR_` + vendorUpper + `
 	bool "` + vendor + `"
 `)
@@ -734,6 +767,7 @@ func main() {
 	if len(BootBlockFiles) > 0 || len(ROMStageFiles) > 0 || len(RAMStageFiles) > 0 || len(SMMFiles) > 0 {
 		mf := Create(ctx, "Makefile.mk")
 		defer mf.Close()
+		Add_SPDX(mf, Makefile, GPL2_only)
 		writeMF(mf, BootBlockFiles, "bootblock")
 		writeMF(mf, ROMStageFiles, "romstage")
 		writeMF(mf, RAMStageFiles, "ramstage")
@@ -749,7 +783,7 @@ func main() {
 	if MainboardInit != "" || MainboardEnable != "" || MainboardIncludes != nil {
 		mainboard := Create(ctx, "mainboard.c")
 		defer mainboard.Close()
-		Add_gpl(mainboard)
+		Add_SPDX(mainboard, C, GPL2_only)
 		mainboard.WriteString("#include <device/device.h>\n")
 		for _, include := range MainboardIncludes {
 			mainboard.WriteString("#include <" + include + ">\n")
@@ -820,7 +854,7 @@ func main() {
 
 	dsdt := Create(ctx, "dsdt.asl")
 	defer dsdt.Close()
-	Add_gpl(dsdt)
+	Add_SPDX(dsdt, ASL, GPL2_only)
 
 	for _, define := range DSDTDefines {
 		if define.Comment != "" {
@@ -870,10 +904,9 @@ DefinitionBlock(
 	if IGDEnabled {
 		gma := Create(ctx, "gma-mainboard.ads")
 		defer gma.Close()
+		Add_SPDX(gma, Ada, GPL2_or_later)
 
-		gma.WriteString(`-- SPDX-License-Identifier: GPL-2.0-or-later
-
-with HW.GFX.GMA;
+		gma.WriteString(`with HW.GFX.GMA;
 with HW.GFX.GMA.Display_Probing;
 
 use HW.GFX.GMA;
-- 
cgit v1.2.3