Skip to content

Commit 14d7cbe

Browse files
committed
big registry and chip package upgrades + build process hardening
1 parent 9702a4f commit 14d7cbe

38 files changed

Lines changed: 3533 additions & 156 deletions

cli/toob-cli/cmd/conformance.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/toob-boot/toob/internal/conformance"
9+
"github.com/toob-boot/toob/internal/ui"
10+
)
11+
12+
var conformanceCmd = &cobra.Command{
13+
Use: "conformance [path]",
14+
Short: "Audit a package for HAL trait conformance and Mock/Real equivalence",
15+
Long: `Audits a driver or chip package directory against HAL trait contracts and Mock vs Real implementation parity.
16+
17+
Generates conformance_report.json and conformance_report.md (CRA Annex-I Evidence).`,
18+
Args: cobra.MaximumNArgs(1),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
pkgPath := "."
21+
if len(args) > 0 {
22+
pkgPath = args[0]
23+
}
24+
absDir, err := filepath.Abs(pkgPath)
25+
if err != nil {
26+
return fmt.Errorf("invalid path: %w", err)
27+
}
28+
29+
ui.Header(fmt.Sprintf("HAL Conformance Harness: %s", filepath.Base(absDir)))
30+
31+
report, err := conformance.AuditPackage(absDir)
32+
if err != nil {
33+
return fmt.Errorf("conformance audit failed: %w", err)
34+
}
35+
36+
jsonPath := filepath.Join(absDir, "conformance_report.json")
37+
mdPath := filepath.Join(absDir, "conformance_report.md")
38+
39+
_ = conformance.ExportReportJSON(report, jsonPath)
40+
_ = conformance.ExportReportMarkdown(report, mdPath)
41+
42+
for _, chk := range report.Checks {
43+
if chk.Passed {
44+
ui.Success("%s: %s", chk.Name, chk.Details)
45+
} else {
46+
ui.Error("%s: %s", chk.Name, chk.Details)
47+
}
48+
}
49+
50+
if !report.Passed {
51+
return fmt.Errorf("FATAL [CONFORMANCE_FAIL]: Package failed HAL conformance audit!")
52+
}
53+
54+
ui.Success("HAL Conformance audit PASSED cleanly. Exported reports to %s and %s", jsonPath, mdPath)
55+
return nil
56+
},
57+
}
58+
59+
func init() {
60+
rootCmd.AddCommand(conformanceCmd)
61+
}

cli/toob-cli/cmd/elfaudit.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strconv"
9+
"strings"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/toob-boot/toob/internal/elfaudit"
13+
"github.com/toob-boot/toob/internal/manifest"
14+
"github.com/toob-boot/toob/internal/ui"
15+
)
16+
17+
var elfauditProfile string
18+
var elfauditHardware string
19+
var elfauditBudget uint64
20+
21+
var elfauditCmd = &cobra.Command{
22+
Use: "elfaudit [path/to/target.elf]",
23+
Short: "Post-link ELF audit: mock poison-pill, memory overlap, budget check",
24+
Long: `Inspects a linked ELF binary for:
25+
(a) Mock Poison-Pill — no *_mock symbols in production profile
26+
(b) Memory Overlap — sections vs. reserved_ram_regions from hardware.json
27+
(c) Budget Footprint — loadable content vs. stage1_size budget`,
28+
Args: cobra.ExactArgs(1),
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
elfPath, err := filepath.Abs(args[0])
31+
if err != nil {
32+
return fmt.Errorf("invalid ELF path: %w", err)
33+
}
34+
35+
if _, err := os.Stat(elfPath); os.IsNotExist(err) {
36+
return fmt.Errorf("ELF binary not found: %s", elfPath)
37+
}
38+
39+
config := elfaudit.ELFAuditConfig{
40+
Profile: elfauditProfile,
41+
Stage1MaxBytes: elfauditBudget,
42+
}
43+
44+
// Load reserved regions from hardware.json if provided
45+
if elfauditHardware != "" {
46+
absHW, err := filepath.Abs(elfauditHardware)
47+
if err != nil {
48+
return fmt.Errorf("invalid hardware.json path: %w", err)
49+
}
50+
data, err := os.ReadFile(absHW)
51+
if err != nil {
52+
return fmt.Errorf("failed to read hardware.json: %w", err)
53+
}
54+
var hj manifest.HardwareJson
55+
if err := json.Unmarshal(data, &hj); err != nil {
56+
return fmt.Errorf("failed to parse hardware.json: %w", err)
57+
}
58+
for _, r := range hj.ReservedRamRegions {
59+
base, err := strconv.ParseUint(strings.TrimPrefix(r.Base, "0x"), 16, 64)
60+
if err != nil {
61+
return fmt.Errorf("invalid base address '%s' for reserved region '%s': %w", r.Base, r.Name, err)
62+
}
63+
config.ReservedRegions = append(config.ReservedRegions, elfaudit.MemoryRegion{
64+
Name: r.Name,
65+
Base: base,
66+
Size: uint64(r.Size),
67+
})
68+
}
69+
}
70+
71+
ui.Header(fmt.Sprintf("ELF Audit: %s [profile=%s]", filepath.Base(elfPath), config.Profile))
72+
73+
report, err := elfaudit.AuditELF(elfPath, config)
74+
if err != nil {
75+
return fmt.Errorf("ELF audit failed: %w", err)
76+
}
77+
78+
for _, chk := range report.Checks {
79+
if chk.Passed {
80+
ui.Success("%s: %s", chk.Name, chk.Details)
81+
} else {
82+
ui.Error("%s: %s", chk.Name, chk.Details)
83+
}
84+
}
85+
86+
// Export JSON report next to the ELF
87+
reportPath := elfPath + ".audit.json"
88+
reportData, _ := json.MarshalIndent(report, "", " ")
89+
_ = os.WriteFile(reportPath, reportData, 0o644)
90+
91+
if !report.Passed {
92+
return fmt.Errorf("FATAL [ELF_AUDIT_FAIL]: Binary failed post-link audit!")
93+
}
94+
95+
ui.Success("ELF audit PASSED cleanly. Report: %s", reportPath)
96+
return nil
97+
},
98+
}
99+
100+
func init() {
101+
elfauditCmd.Flags().StringVar(&elfauditProfile, "profile", "production", "Build profile (production|sandbox)")
102+
elfauditCmd.Flags().StringVar(&elfauditHardware, "hardware", "", "Path to hardware.json for reserved region checks")
103+
elfauditCmd.Flags().Uint64Var(&elfauditBudget, "budget", 0, "Stage1 size budget in bytes (0 = skip budget check)")
104+
rootCmd.AddCommand(elfauditCmd)
105+
}
106+

cli/toob-cli/cmd/literallint.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/toob-boot/toob/internal/literallint"
11+
"github.com/toob-boot/toob/internal/ui"
12+
)
13+
14+
var (
15+
literallintDeep bool
16+
literallintIncludes []string
17+
literallintJSON string
18+
)
19+
20+
var literallintCmd = &cobra.Command{
21+
Use: "literallint [paths...]",
22+
Short: "Literal-Bann-Lint for chips/ and drivers/ codebases",
23+
Long: `Audits C/H source files for unallowed numeric literals (hardcoded addresses, register offsets, magic numbers).
24+
Every hardware constant must originate from generated_boot_config.h or chip_config.h.
25+
26+
Uses a regex scanner by default, and optional AST inspection via clang-query when --deep is specified.`,
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
paths := args
29+
if len(paths) == 0 {
30+
// Default paths
31+
paths = []string{
32+
"toob-registry/registry/chips",
33+
"toob-registry/registry/drivers",
34+
}
35+
}
36+
37+
var absPaths []string
38+
for _, p := range paths {
39+
abs, err := filepath.Abs(p)
40+
if err != nil {
41+
return fmt.Errorf("invalid path %s: %w", p, err)
42+
}
43+
if _, err := os.Stat(abs); err == nil {
44+
absPaths = append(absPaths, abs)
45+
}
46+
}
47+
48+
if len(absPaths) == 0 {
49+
return fmt.Errorf("no valid scanning paths found among: %v", paths)
50+
}
51+
52+
cfg := literallint.LintConfig{
53+
Paths: absPaths,
54+
Extensions: []string{".c", ".h"},
55+
IncludePaths: literallintIncludes,
56+
DeepMode: literallintDeep,
57+
}
58+
59+
ui.Header(fmt.Sprintf("Literal-Bann-Lint: Scanning %d path(s)", len(absPaths)))
60+
61+
report, err := literallint.RunLint(cfg)
62+
if err != nil {
63+
return fmt.Errorf("literal lint failed: %w", err)
64+
}
65+
66+
ui.Info("Mode: %s", report.Mode)
67+
ui.Info("Files scanned: %d, Lines scanned: %d, Suppressed: %d",
68+
report.Stats.FilesScanned, report.Stats.LinesScanned, report.Stats.Suppressed)
69+
70+
if len(report.Violations) > 0 {
71+
ui.Error("Found %d numeric literal violation(s):", len(report.Violations))
72+
for _, v := range report.Violations {
73+
relPath, _ := filepath.Rel(".", v.File)
74+
if relPath == "" {
75+
relPath = v.File
76+
}
77+
ui.Error(" %s:%d:%d: literal '%s' [%s]\n Line: %s",
78+
relPath, v.Line, v.Column, v.Literal, v.Source, v.Context)
79+
}
80+
}
81+
82+
if literallintJSON != "" {
83+
data, _ := json.MarshalIndent(report, "", " ")
84+
if err := os.WriteFile(literallintJSON, data, 0o644); err != nil {
85+
ui.Error("Failed to write JSON report to %s: %v", literallintJSON, err)
86+
} else {
87+
ui.Success("Exported JSON report to %s", literallintJSON)
88+
}
89+
}
90+
91+
if !report.Passed {
92+
return fmt.Errorf("FATAL [LITERAL_LINT_FAIL]: Found %d banned numeric literal(s)!", len(report.Violations))
93+
}
94+
95+
ui.Success("Literal-Bann-Lint PASSED cleanly. No unauthorized numeric literals found.")
96+
return nil
97+
},
98+
}
99+
100+
func init() {
101+
literallintCmd.Flags().BoolVar(&literallintDeep, "deep", false, "Enable clang-query AST hybrid scanning")
102+
literallintCmd.Flags().StringSliceVar(&literallintIncludes, "include", nil, "Header include paths for clang-query")
103+
literallintCmd.Flags().StringVar(&literallintJSON, "json", "", "Export report as JSON to specified file path")
104+
rootCmd.AddCommand(literallintCmd)
105+
}

cli/toob-cli/cmd/publish.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/spf13/cobra"
1717
"github.com/toob-boot/toob/internal/apiclient"
18+
"github.com/toob-boot/toob/internal/conformance"
1819
"github.com/toob-boot/toob/internal/paths"
1920
"github.com/toob-boot/toob/internal/registry"
2021
"github.com/toob-boot/toob/internal/ui"
@@ -67,6 +68,18 @@ Examples:
6768
if err := checkManifestDependencies(cmd_defaultCtx(), absDir, client); err != nil {
6869
return err
6970
}
71+
72+
// Mandatory HAL Conformance Gate (REG-042)
73+
ui.Step("Running HAL Conformance Gate...")
74+
confReport, confErr := conformance.AuditPackage(absDir)
75+
if confErr != nil || confReport == nil || !confReport.Passed {
76+
return fmt.Errorf("FATAL [CONFORMANCE_FAIL]: Package failed HAL conformance audit! Run 'toob conformance %s' for details.", absDir)
77+
}
78+
jsonPath := filepath.Join(absDir, "conformance_report.json")
79+
mdPath := filepath.Join(absDir, "conformance_report.md")
80+
_ = conformance.ExportReportJSON(confReport, jsonPath)
81+
_ = conformance.ExportReportMarkdown(confReport, mdPath)
82+
ui.Success("HAL Conformance audit PASSED cleanly. Generated CRA compliance reports.")
7083
// Name collision pre-check (Gap 14)
7184
manifestName := detectPackageName(absDir)
7285
if manifestName != "" {

cli/toob-cli/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313

1414
require (
1515
aead.dev/minisign v0.3.0 // indirect
16-
golang.org/x/sync v0.22.0 // indirect
16+
golang.org/x/sync v0.22.0
1717
)
1818

1919
require (

0 commit comments

Comments
 (0)