func (nv *NVRAM) setAttributes() error { if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) { return Error{result} } if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_PERMISSIONS, 0, C.UINT32(nv.Permissions)); isError(result) { return Error{result} } if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_DATASIZE, 0, C.UINT32(nv.Size)); isError(result) { return Error{result} } return nil }
func (nv *NVRAM) Write(contents []byte) error { if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) { return Error{result} } if result := C.Tspi_NV_WriteValue(C.TSS_HNVSTORE(nv.handle), 0 /* offset */, C.UINT32(len(contents)), (*C.BYTE)(&contents[0])); isError(result) { return Error{result} } return nil }
func (nv *NVRAM) Read(out []byte) (int, error) { if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) { return 0, Error{result} } l32 := C.UINT32(len(out)) var buf *C.BYTE if result := C.Tspi_NV_ReadValue(C.TSS_HNVSTORE(nv.handle), 0 /* offset */, &l32, &buf); isError(result) { return 0, Error{result} } l := int(l32) if l > len(out) { l = len(out) } if l < 0 { l = 0 } C.memcpy(unsafe.Pointer(&out[0]), unsafe.Pointer(buf), C.size_t(l)) return l, nil }
// SetIndex sets the TPM NVRAM index that will be referenced by ReadValue() func (nv *NV) SetIndex(index uint) error { err := tspiError(C.Tspi_SetAttribUint32((C.TSS_HOBJECT)(nv.handle), C.TSS_TSPATTRIB_NV_INDEX, 0, (C.UINT32)(index))) return err }