I'm having one of those "I hate this all" moments. Look at what we say
in the code:
> * See the "global_metadata.json" in the "TDX 1.5 ABI definitions".
Basically step one in verifying that this is all right is: Hey, humans,
please go parse a machine-readable format. That's insanity. If Intel
wants to publish JSON as the canonical source of truth, that's fine.
It's great, actually. But let's stop playing human JSON parser and make
the computers do it for us, OK?
Let's just generate the code. Basically, as long as the generated C is
marginally readable, I'm OK with it. The most important things are:
1. Adding a field is dirt simple
2. Using the generated C is simple
In 99% of the cases, nobody ends up having to ever look at the generated
code.
Take a look at the attached python program and generated C file. I
think they qualify. We can check the script into tools/scripts/ and it
can get re-run when new json comes out or when a new field is needed.
You'd could call the generated code like this:
#include <generated.h>
read_gunk(&tgm);
and use it like this:
foo = tgm.BUILD_NUM;
bar = tgm.BUILD_DATE;
Any field you want to add is a single addition to the python list and
re-running the script. There's not even any need to do:
#define TDX_FOO_BAR_BUILD_DATE 0x8800000200000001
because it's unnecessary when you have:
ret |= read_...(0x8800000200000001, &tgm.BUILD_DATE);
that links the magic number and the "BUILD_DATE" so closely together
anyway. We also don't need type safety *here* at the "read" because
it's machine generated in the first place. If there's a type mismatch
between "0x8800000200000001" and "tgm.BUILD_DATE" we have bigger
problems on our hands.
All the type checking comes when the code consumes tgm.BUILD_DATE (or
whatever).
#!/usr/bin/python3
import json
import sys
filefd = open(sys.argv[1])
jsonstr = filefd.read()
filefd.close()
j = json.loads(jsonstr)
print("static struct tdx_global_metadata tgm")
print("{")
def find_field(name):
for f in j['Fields']:
if f['Field Name'] == name:
return f
return None
fields = """
TDX_FEATURES0
BUILD_DATE
BUILD_NUM
MINOR_VERSION
""".strip().split("\n")
for fn in fields:
f = find_field(fn)
name = f['Field Name']
element_bytes = int(f['Element Size (Bytes)'])
element_bits = element_bytes * 8
print("\tu%d %s;" % (element_bits, name))
print("}")
print("static void read_gunk()")
print("{")
print("\tint ret = 0;")
print("")
for fn in fields:
f = find_field(fn)
print("\tret |= read_sys_metadata_field(%s, &tgm.%s);" %
(f['Base FIELD_ID (Hex)'],
f['Field Name']))
print("")
print("\treturn ret;")
print("}")
static struct tdx_global_metadata tgm
{
u64 TDX_FEATURES0;
u32 BUILD_DATE;
u16 BUILD_NUM;
u16 MINOR_VERSION;
}
static void read_gunk()
{
int ret = 0;
ret |= read_sys_metadata_field(0x0A00000300000008, &tgm.TDX_FEATURES0);
ret |= read_sys_metadata_field(0x8800000200000001, &tgm.BUILD_DATE);
ret |= read_sys_metadata_field(0x8800000100000002, &tgm.BUILD_NUM);
ret |= read_sys_metadata_field(0x0800000100000003, &tgm.MINOR_VERSION);
return ret;
}