Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit 0e1f567

Browse files
committed
Accept memory in bytes
1 parent 9d42fd5 commit 0e1f567

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

internal/acpi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ func GenerateTablesQemu(memorySize uint64, cpuCount uint8) ([]byte, []byte, []by
6363
rangeMinimumOffset := lengthOffset - 12
6464

6565
// Handle memory split at 2816 MiB (0xB0000000).
66-
if memorySize >= 2816 {
66+
if memorySize >= 0xB0000000 {
6767
binary.LittleEndian.PutUint32(tpl[rangeMinimumOffset:], 0x80000000)
6868
binary.LittleEndian.PutUint32(tpl[lengthOffset:], 0x60000000)
6969
} else {
70-
memSizeBytes := uint32(memorySize * 1024 * 1024)
70+
memSizeBytes := uint32(memorySize)
7171
binary.LittleEndian.PutUint32(tpl[rangeMinimumOffset:], memSizeBytes)
7272
binary.LittleEndian.PutUint32(tpl[lengthOffset:], 0xe0000000-memSizeBytes)
7373
}

internal/mr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func measureTdxQemuTdHob(memorySize uint64, meta *tdvfMetadata) []byte {
6464
)
6565

6666
// The rest of the HOBs are EFI_HOB_TYPE_RESOURCE_DESCRIPTOR.
67-
remainingMemory := memorySize * 1024 * 1024 // Convert to bytes.
67+
remainingMemory := memorySize
6868
addMemoryResourceHob := func(resourceType uint8, start, length uint64) {
6969
tdHob = append(tdHob,
7070
0x03, 0x00, // Header.HobType (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR)
@@ -94,7 +94,7 @@ func measureTdxQemuTdHob(memorySize uint64, meta *tdvfMetadata) []byte {
9494
addMemoryResourceHob(0x00, 0x0000000000811000, 0x000000000000f000)
9595

9696
// Handle memory split at 2816 MiB (0xB0000000).
97-
if memorySize >= 2816 {
97+
if memorySize >= 0xB0000000 {
9898
addMemoryResourceHob(0x07, 0x0000000000820000, 0x000000007F7E0000)
9999
addMemoryResourceHob(0x07, 0x0000000100000000, remainingMemory)
100100
} else {

main.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,30 @@ type measurementOutput struct {
2828
MrImage string `json:"mr_image"`
2929
}
3030

31+
const (
32+
GB = 1024 * 1024 * 1024 // in bytes
33+
MB = 1024 * 1024
34+
)
35+
3136
var knownKeyProviders = map[string]string{
3237
"sgx-v0": "0x4888adb026ff91c1320c4f544a9f5d9e0561e13fc64947a10aa1556d0071b2cc",
3338
"none": "0x3369c4d32b9f1320ebba5ce9892a283127b7e96e1d511d7f292e5d9ed2c10b8c",
3439
}
3540

36-
// parseMemorySize parses a human readable memory size (e.g., "1G", "512M") into megabytes
41+
// parseMemorySize parses a human readable memory size (e.g., "1G", "512M") into bytes
3742
func parseMemorySize(size string) (uint64, error) {
3843
size = strings.TrimSpace(strings.ToUpper(size))
3944
if len(size) == 0 {
4045
return 0, fmt.Errorf("empty memory size")
4146
}
4247

48+
// Check if the input is purely numeric (no unit)
49+
if _, err := strconv.ParseUint(size, 10, 64); err == nil {
50+
// If it's a valid number with no unit, interpret as MB
51+
num, _ := strconv.ParseUint(size, 10, 64)
52+
return num, nil
53+
}
54+
4355
// Get the unit (last character)
4456
unit := size[len(size)-1:]
4557
// Get the number (everything except the last character)
@@ -51,12 +63,12 @@ func parseMemorySize(size string) (uint64, error) {
5163
return 0, fmt.Errorf("invalid memory size number: %v", err)
5264
}
5365

54-
// Convert to megabytes based on unit
66+
// Convert to bytes based on unit
5567
switch unit {
5668
case "G":
57-
return num * 1024, nil // Convert GB to MB
69+
return num * GB, nil // Convert GB to bytes
5870
case "M":
59-
return num, nil // Already in MB
71+
return num * MB, nil // Convert MB to bytes
6072
default:
6173
return 0, fmt.Errorf("invalid memory unit '%s', must be one of: G, M", unit)
6274
}
@@ -65,23 +77,22 @@ func parseMemorySize(size string) (uint64, error) {
6577
type memoryValue uint64
6678

6779
func (m *memoryValue) String() string {
68-
mb := uint64(*m)
69-
const (
70-
GB = 1024 // in MB
71-
)
72-
73-
if mb >= GB && mb%GB == 0 {
74-
return fmt.Sprintf("%dG", mb/GB)
80+
m_bytes := uint64(*m)
81+
if m_bytes >= GB && m_bytes%GB == 0 {
82+
return fmt.Sprintf("%dG", m_bytes/GB)
83+
}
84+
if m_bytes >= MB && m_bytes%MB == 0 {
85+
return fmt.Sprintf("%dM", m_bytes/MB)
7586
}
76-
return fmt.Sprintf("%dM", mb)
87+
return fmt.Sprintf("%d", m_bytes)
7788
}
7889

7990
func (m *memoryValue) Set(value string) error {
80-
mb, err := parseMemorySize(value)
91+
m_bytes, err := parseMemorySize(value)
8192
if err != nil {
8293
return err
8394
}
84-
*m = memoryValue(mb)
95+
*m = memoryValue(m_bytes)
8596
return nil
8697
}
8798

@@ -91,7 +102,7 @@ func main() {
91102
fwPath string
92103
kernelPath string
93104
initrdPath string
94-
memorySize memoryValue = 2048 // 2G default (in MB)
105+
memorySize memoryValue
95106
cpuCountUint uint
96107
kernelCmdline string
97108
jsonOutput bool

0 commit comments

Comments
 (0)