Start rewriting other NVRAM parsers in KaitaiStruct

This commit is contained in:
Nikolaj Schlej 2023-02-19 18:47:19 -08:00
parent 6c695c12a7
commit 318fdb7fa1
7 changed files with 590 additions and 1554 deletions

View File

@ -71,6 +71,7 @@ SET(PROJECT_SOURCES
../common/digest/sha512.c ../common/digest/sha512.c
../common/digest/sm3.c ../common/digest/sm3.c
../common/generated/ami_nvar.cpp ../common/generated/ami_nvar.cpp
../common/generated/edk2_vss.cpp
../common/generated/intel_acbp_v1.cpp ../common/generated/intel_acbp_v1.cpp
../common/generated/intel_acbp_v2.cpp ../common/generated/intel_acbp_v2.cpp
../common/generated/intel_keym_v1.cpp ../common/generated/intel_keym_v1.cpp

View File

@ -1428,7 +1428,7 @@ USTATUS FfsParser::parseVolumeBody(const UModelIndex & index)
UByteArray volumeBody = model->body(index); UByteArray volumeBody = model->body(index);
UINT32 volumeHeaderSize = (UINT32)model->header(index).size(); UINT32 volumeHeaderSize = (UINT32)model->header(index).size();
// Parse VSS NVRAM volumes with a dedicated function // Parse NVRAM volumes with a dedicated function
if (model->subtype(index) == Subtypes::NvramVolume) { if (model->subtype(index) == Subtypes::NvramVolume) {
return nvramParser->parseNvramVolumeBody(index); return nvramParser->parseNvramVolumeBody(index);
} }

View File

@ -0,0 +1,234 @@
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "edk2_vss.h"
#include "../kaitai/exceptions.h"
edk2_vss_t::edk2_vss_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, edk2_vss_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = this; (void)p__root;
m_body = 0;
m__io__raw_body = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void edk2_vss_t::_read() {
m_signature = m__io->read_u4le();
{
uint32_t _ = signature();
if (!( ((_ == 1397970468) || (_ == 1398166308) || (_ == 1397968420)) )) {
throw kaitai::validation_expr_error<uint32_t>(signature(), _io(), std::string("/seq/0"));
}
}
m_size = m__io->read_u4le();
{
uint32_t _ = size();
if (!(_ > (4 * 4))) {
throw kaitai::validation_expr_error<uint32_t>(size(), _io(), std::string("/seq/1"));
}
}
m_format = m__io->read_u1();
{
uint8_t _ = format();
if (!(_ == 90)) {
throw kaitai::validation_expr_error<uint8_t>(format(), _io(), std::string("/seq/2"));
}
}
m_state = m__io->read_u1();
m_reserved = m__io->read_u2le();
m_reserved1 = m__io->read_u4le();
m__raw_body = m__io->read_bytes((size() - (4 * 4)));
m__io__raw_body = new kaitai::kstream(m__raw_body);
m_body = new vss_store_body_t(m__io__raw_body, this, m__root);
}
edk2_vss_t::~edk2_vss_t() {
_clean_up();
}
void edk2_vss_t::_clean_up() {
if (m__io__raw_body) {
delete m__io__raw_body; m__io__raw_body = 0;
}
if (m_body) {
delete m_body; m_body = 0;
}
}
edk2_vss_t::vss_store_body_t::vss_store_body_t(kaitai::kstream* p__io, edk2_vss_t* p__parent, edk2_vss_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
m_variables = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void edk2_vss_t::vss_store_body_t::_read() {
m_variables = new std::vector<vss_variable_t*>();
{
int i = 0;
vss_variable_t* _;
do {
_ = new vss_variable_t(m__io, this, m__root);
m_variables->push_back(_);
i++;
} while (!( ((_->signature_first() != 170) || (_io()->is_eof())) ));
}
}
edk2_vss_t::vss_store_body_t::~vss_store_body_t() {
_clean_up();
}
void edk2_vss_t::vss_store_body_t::_clean_up() {
if (m_variables) {
for (std::vector<vss_variable_t*>::iterator it = m_variables->begin(); it != m_variables->end(); ++it) {
delete *it;
}
delete m_variables; m_variables = 0;
}
}
edk2_vss_t::vss_variable_attributes_t::vss_variable_attributes_t(kaitai::kstream* p__io, edk2_vss_t::vss_variable_t* p__parent, edk2_vss_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void edk2_vss_t::vss_variable_attributes_t::_read() {
m_non_volatile = m__io->read_bits_int_le(1);
m_boot_service = m__io->read_bits_int_le(1);
m_runtime = m__io->read_bits_int_le(1);
m_hw_error_record = m__io->read_bits_int_le(1);
m_auth_write = m__io->read_bits_int_le(1);
m_time_based_auth = m__io->read_bits_int_le(1);
m_append_write = m__io->read_bits_int_le(1);
m_reserved = m__io->read_bits_int_le(24);
m_apple_data_checksum = m__io->read_bits_int_le(1);
}
edk2_vss_t::vss_variable_attributes_t::~vss_variable_attributes_t() {
_clean_up();
}
void edk2_vss_t::vss_variable_attributes_t::_clean_up() {
}
edk2_vss_t::vss_variable_t::vss_variable_t(kaitai::kstream* p__io, edk2_vss_t::vss_store_body_t* p__parent, edk2_vss_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
m_attributes = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
void edk2_vss_t::vss_variable_t::_read() {
m_signature_first = m__io->read_u1();
n_signature_last = true;
if (signature_first() == 170) {
n_signature_last = false;
m_signature_last = m__io->read_u1();
{
uint8_t _ = signature_last();
if (!(_ == 85)) {
throw kaitai::validation_expr_error<uint8_t>(signature_last(), _io(), std::string("/types/vss_variable/seq/1"));
}
}
}
n_state = true;
if (signature_first() == 170) {
n_state = false;
m_state = m__io->read_u1();
}
n_reserved = true;
if (signature_first() == 170) {
n_reserved = false;
m_reserved = m__io->read_u1();
}
n_attributes = true;
if (signature_first() == 170) {
n_attributes = false;
m_attributes = new vss_variable_attributes_t(m__io, this, m__root);
}
n_len_name = true;
if (signature_first() == 170) {
n_len_name = false;
m_len_name = m__io->read_u4le();
}
n_len_data = true;
if (signature_first() == 170) {
n_len_data = false;
m_len_data = m__io->read_u4le();
}
n_vendor_guid = true;
if (signature_first() == 170) {
n_vendor_guid = false;
m_vendor_guid = m__io->read_bytes(16);
}
n_apple_data_crc32 = true;
if ( ((signature_first() == 170) && (attributes()->apple_data_checksum())) ) {
n_apple_data_crc32 = false;
m_apple_data_crc32 = m__io->read_u4le();
}
n_name = true;
if (signature_first() == 170) {
n_name = false;
m_name = m__io->read_bytes(len_name());
}
n_data = true;
if (signature_first() == 170) {
n_data = false;
m_data = m__io->read_bytes(len_data());
}
}
edk2_vss_t::vss_variable_t::~vss_variable_t() {
_clean_up();
}
void edk2_vss_t::vss_variable_t::_clean_up() {
if (!n_signature_last) {
}
if (!n_state) {
}
if (!n_reserved) {
}
if (!n_attributes) {
if (m_attributes) {
delete m_attributes; m_attributes = 0;
}
}
if (!n_len_name) {
}
if (!n_len_data) {
}
if (!n_vendor_guid) {
}
if (!n_apple_data_crc32) {
}
if (!n_name) {
}
if (!n_data) {
}
}

225
common/generated/edk2_vss.h Normal file
View File

@ -0,0 +1,225 @@
#ifndef EDK2_VSS_H_
#define EDK2_VSS_H_
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "../kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>
#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif
class edk2_vss_t : public kaitai::kstruct {
public:
class vss_store_body_t;
class vss_variable_attributes_t;
class vss_variable_t;
edk2_vss_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, edk2_vss_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~edk2_vss_t();
class vss_store_body_t : public kaitai::kstruct {
public:
vss_store_body_t(kaitai::kstream* p__io, edk2_vss_t* p__parent = 0, edk2_vss_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~vss_store_body_t();
private:
std::vector<vss_variable_t*>* m_variables;
edk2_vss_t* m__root;
edk2_vss_t* m__parent;
public:
std::vector<vss_variable_t*>* variables() const { return m_variables; }
edk2_vss_t* _root() const { return m__root; }
edk2_vss_t* _parent() const { return m__parent; }
};
class vss_variable_attributes_t : public kaitai::kstruct {
public:
vss_variable_attributes_t(kaitai::kstream* p__io, edk2_vss_t::vss_variable_t* p__parent = 0, edk2_vss_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~vss_variable_attributes_t();
private:
bool m_non_volatile;
bool m_boot_service;
bool m_runtime;
bool m_hw_error_record;
bool m_auth_write;
bool m_time_based_auth;
bool m_append_write;
uint64_t m_reserved;
bool m_apple_data_checksum;
edk2_vss_t* m__root;
edk2_vss_t::vss_variable_t* m__parent;
public:
bool non_volatile() const { return m_non_volatile; }
bool boot_service() const { return m_boot_service; }
bool runtime() const { return m_runtime; }
bool hw_error_record() const { return m_hw_error_record; }
bool auth_write() const { return m_auth_write; }
bool time_based_auth() const { return m_time_based_auth; }
bool append_write() const { return m_append_write; }
uint64_t reserved() const { return m_reserved; }
bool apple_data_checksum() const { return m_apple_data_checksum; }
edk2_vss_t* _root() const { return m__root; }
edk2_vss_t::vss_variable_t* _parent() const { return m__parent; }
};
class vss_variable_t : public kaitai::kstruct {
public:
vss_variable_t(kaitai::kstream* p__io, edk2_vss_t::vss_store_body_t* p__parent = 0, edk2_vss_t* p__root = 0);
private:
void _read();
void _clean_up();
public:
~vss_variable_t();
private:
uint8_t m_signature_first;
uint8_t m_signature_last;
bool n_signature_last;
public:
bool _is_null_signature_last() { signature_last(); return n_signature_last; };
private:
uint8_t m_state;
bool n_state;
public:
bool _is_null_state() { state(); return n_state; };
private:
uint8_t m_reserved;
bool n_reserved;
public:
bool _is_null_reserved() { reserved(); return n_reserved; };
private:
vss_variable_attributes_t* m_attributes;
bool n_attributes;
public:
bool _is_null_attributes() { attributes(); return n_attributes; };
private:
uint32_t m_len_name;
bool n_len_name;
public:
bool _is_null_len_name() { len_name(); return n_len_name; };
private:
uint32_t m_len_data;
bool n_len_data;
public:
bool _is_null_len_data() { len_data(); return n_len_data; };
private:
std::string m_vendor_guid;
bool n_vendor_guid;
public:
bool _is_null_vendor_guid() { vendor_guid(); return n_vendor_guid; };
private:
uint32_t m_apple_data_crc32;
bool n_apple_data_crc32;
public:
bool _is_null_apple_data_crc32() { apple_data_crc32(); return n_apple_data_crc32; };
private:
std::string m_name;
bool n_name;
public:
bool _is_null_name() { name(); return n_name; };
private:
std::string m_data;
bool n_data;
public:
bool _is_null_data() { data(); return n_data; };
private:
edk2_vss_t* m__root;
edk2_vss_t::vss_store_body_t* m__parent;
public:
uint8_t signature_first() const { return m_signature_first; }
uint8_t signature_last() const { return m_signature_last; }
uint8_t state() const { return m_state; }
uint8_t reserved() const { return m_reserved; }
vss_variable_attributes_t* attributes() const { return m_attributes; }
uint32_t len_name() const { return m_len_name; }
uint32_t len_data() const { return m_len_data; }
std::string vendor_guid() const { return m_vendor_guid; }
uint32_t apple_data_crc32() const { return m_apple_data_crc32; }
std::string name() const { return m_name; }
std::string data() const { return m_data; }
edk2_vss_t* _root() const { return m__root; }
edk2_vss_t::vss_store_body_t* _parent() const { return m__parent; }
};
private:
uint32_t m_signature;
uint32_t m_size;
uint8_t m_format;
uint8_t m_state;
uint16_t m_reserved;
uint32_t m_reserved1;
vss_store_body_t* m_body;
edk2_vss_t* m__root;
kaitai::kstruct* m__parent;
std::string m__raw_body;
kaitai::kstream* m__io__raw_body;
public:
uint32_t signature() const { return m_signature; }
uint32_t size() const { return m_size; }
uint8_t format() const { return m_format; }
uint8_t state() const { return m_state; }
uint16_t reserved() const { return m_reserved; }
uint32_t reserved1() const { return m_reserved1; }
vss_store_body_t* body() const { return m_body; }
edk2_vss_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
std::string _raw_body() const { return m__raw_body; }
kaitai::kstream* _io__raw_body() const { return m__io__raw_body; }
};
#endif // EDK2_VSS_H_

100
common/ksy/edk2_vss.ksy Normal file
View File

@ -0,0 +1,100 @@
meta:
id: edk2_vss
title: EDK2 VSS storage
application: EDK2-based UEFI firmware
file-extension: vss
tags:
- firmware
license: CC0-1.0
ks-version: 0.9
endian: le
seq:
- id: signature
type: u4
valid:
expr: _ == 0x53535624 or _ == 0x53565324 or _ == 0x53534E24 # $VSS/$SVS/$NSS
- id: size
type: u4
valid:
expr: _ > 4 * sizeof<u4>
- id: format
type: u1
valid:
expr: _ == 0x5a # Formatted
- id: state
type: u1
- id: reserved
type: u2
- id: reserved1
type: u4
- id: body
type: vss_store_body
size: size - 4 * sizeof<u4>
types:
vss_store_body:
seq:
- id: variables
type: vss_variable
repeat: until
repeat-until: _.signature_first != 0xAA or _io.eof
vss_variable_attributes:
seq:
- id: non_volatile
type: b1le
- id: boot_service
type: b1le
- id: runtime
type: b1le
- id: hw_error_record
type: b1le
- id: auth_write
type: b1le
- id: time_based_auth
type: b1le
- id: append_write
type: b1le
- id: reserved
type: b24le
- id: apple_data_checksum
type: b1le
vss_variable:
seq:
- id: signature_first
type: u1
- id: signature_last
type: u1
valid:
expr: _ == 0x55
if: signature_first == 0xAA
- id: state
type: u1
if: signature_first == 0xAA
- id: reserved
type: u1
if: signature_first == 0xAA
- id: attributes
type: vss_variable_attributes
if: signature_first == 0xAA
#TODO: add Intel legacy total_size variant
- id: len_name
type: u4
if: signature_first == 0xAA
- id: len_data
type: u4
if: signature_first == 0xAA
- id: vendor_guid
size: 16
if: signature_first == 0xAA
- id: apple_data_crc32
type: u4
if: signature_first == 0xAA and attributes.apple_data_checksum
- id: name
size: len_name
if: signature_first == 0xAA
- id: data
size: len_data
if: signature_first == 0xAA

File diff suppressed because it is too large Load Diff

View File

@ -47,27 +47,6 @@ private:
void msg(const UString & message, const UModelIndex & index = UModelIndex()) { void msg(const UString & message, const UModelIndex & index = UModelIndex()) {
messagesVector.push_back(std::pair<UString, UModelIndex>(message, index)); messagesVector.push_back(std::pair<UString, UModelIndex>(message, index));
}; };
USTATUS findNextStore(const UModelIndex & index, const UByteArray & volume, const UINT32 localOffset, const UINT32 storeOffset, UINT32 & nextStoreOffset);
USTATUS getStoreSize(const UByteArray & data, const UINT32 storeOffset, UINT32 & storeSize);
USTATUS parseStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseVssStoreHeader(const UByteArray & store, const UINT32 localOffset, const bool sizeOverride, const UModelIndex & parent, UModelIndex & index);
USTATUS parseVss2StoreHeader(const UByteArray & store, const UINT32 localOffset, const bool sizeOverride, const UModelIndex & parent, UModelIndex & index);
USTATUS parseFtwStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseFdcStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseFsysStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseEvsaStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseFlashMapStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseCmdbStoreHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseSlicPubkeyHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseSlicMarkerHeader(const UByteArray & store, const UINT32 localOffset, const UModelIndex & parent, UModelIndex & index);
USTATUS parseFdcStoreBody(const UModelIndex & index);
USTATUS parseVssStoreBody(const UModelIndex & index, const UINT8 alignment);
USTATUS parseFsysStoreBody(const UModelIndex & index);
USTATUS parseEvsaStoreBody(const UModelIndex & index);
USTATUS parseFlashMapBody(const UModelIndex & index);
}; };
#else #else
class NvramParser class NvramParser