add "avbVerifier" to emulate boot time AVB flow
In memory of Dr. Li: He who holds the firewood for the masses, is the one who freezes to death in wind and snow.pull/41/head
parent
ef02d54a78
commit
e5b03661f8
@ -0,0 +1,34 @@
|
||||
.PHONY: clean nativeLibrary
|
||||
.DEFAULT_GOAL: nativeLibrary
|
||||
|
||||
src_dir := src/avbx/cpp
|
||||
header_dir := src/avbx/headers
|
||||
build_dir := build
|
||||
CFLAGS := @build/tmp/compileAvbxStaticLibraryAvbxCpp/options.txt
|
||||
LD_FLAGS := -lavb -L ../aosp/libavb/build/libs/avb/shared
|
||||
|
||||
CPP_FILES := $(wildcard $(src_dir)/*.cpp)
|
||||
OBJ_FILES := $(patsubst $(src_dir)/%,build/%,$(CPP_FILES:.cpp=.o))
|
||||
|
||||
clean:
|
||||
rm -fr $(build_dir)
|
||||
|
||||
build/%.o: $(src_dir)/%.cpp
|
||||
mkdir -p $(dir $@)
|
||||
$(CXX) -fPIC $(CFLAGS) -o $@ $<
|
||||
|
||||
build/libavbxShared.so: $(OBJ_FILES)
|
||||
$(CXX) -shared -fPIC $^ $(LD_FLAGS) -o $@
|
||||
|
||||
build/libavbxStatic.a: $(OBJ_FILES)
|
||||
$(warning $(OBJ_FILES))
|
||||
ar rcs $@ $^
|
||||
|
||||
build/avbxVerifier.o: src/avbVerifier/cpp/main.cpp
|
||||
$(CXX) $^ @build/tmp/compileAvbVerifierExecutableAvbVerifierCpp/options.txt -o $@
|
||||
build/avbxVerifier: build/libavbxStatic.a
|
||||
build/avbxVerifier: build/avbxVerifier.o
|
||||
$(CXX) $< \
|
||||
-lavb -L ../aosp/libavb/build/libs/avb/static \
|
||||
-lavbxStatic -L build \
|
||||
-o $@
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* main.cpp
|
||||
* Copyright (C) 2020 yu <yu@X.local>
|
||||
*
|
||||
* Distributed under terms of the MIT license.
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
#include "CfigAvbOps.h"
|
||||
#include "helper.hpp"
|
||||
|
||||
std::vector<std::string> splitString(const std::string& subject) {
|
||||
static const std::regex re{"\\s+"};
|
||||
std::vector<std::string> container{
|
||||
std::sregex_token_iterator(subject.begin(), subject.end(), re, -1),
|
||||
std::sregex_token_iterator()
|
||||
};
|
||||
return container;
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
auto cfigOps = CfigAvbOps();
|
||||
auto preloads = getenv("preload");
|
||||
if (preloads == NULL) {
|
||||
} else {
|
||||
auto preloadVec = splitString(preloads);
|
||||
for (auto item: preloadVec) {
|
||||
cfigOps.preload_partition(item);
|
||||
}
|
||||
}
|
||||
|
||||
cfigOps.preload_partition("vbmeta");
|
||||
AvbSlotVerifyData *slotData = NULL;
|
||||
const char* requestedPartitions[] = { (const char*) "boot", (const char*)NULL };
|
||||
AvbSlotVerifyResult result = avb_slot_verify(
|
||||
&(cfigOps.avb_ops_),
|
||||
requestedPartitions,
|
||||
"",
|
||||
AVB_SLOT_VERIFY_FLAGS_NONE,
|
||||
AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
|
||||
&slotData);
|
||||
std::cout << "AvbSlotVerifyResult = " << toString(result) << std::endl;
|
||||
if (AVB_SLOT_VERIFY_RESULT_OK == result) {
|
||||
auto outFile = "verify_result.json";
|
||||
std::cout << "Writing result to " << outFile << "... ";
|
||||
std::ofstream outJson(outFile);
|
||||
outJson << toString(slotData);
|
||||
outJson.close();
|
||||
std::cout << " done" << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* helper.cpp
|
||||
* Copyright (C) 2020 yu <yu@X.local>
|
||||
*
|
||||
* Distributed under terms of the MIT license.
|
||||
*/
|
||||
|
||||
#include "helper.hpp"
|
||||
#include <sstream>
|
||||
|
||||
std::string toString(const AvbSlotVerifyData* slotData) {
|
||||
if (!slotData) {
|
||||
return "{}";
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << "{";
|
||||
ss << "\"ab_suffix\":\"" << slotData->ab_suffix << "\",";
|
||||
ss << "\"num_vbmeta_images\":" << slotData->num_vbmeta_images << ",";
|
||||
|
||||
ss << "\"vbmeta_images\":[";
|
||||
for (int i = 0; i < slotData->num_vbmeta_images; i++) {
|
||||
ss << toString(&((slotData->vbmeta_images)[i]));
|
||||
ss << ((i == slotData->num_vbmeta_images - 1) ? "" : ",");
|
||||
}
|
||||
ss << "],";
|
||||
|
||||
ss << "\"num_loaded_partition\":\"" << slotData->num_loaded_partitions << "\",";
|
||||
|
||||
ss << "\"loaded_partitions\":[";
|
||||
for (int i = 0; i < slotData->num_loaded_partitions; i++) {
|
||||
ss << toString(&((slotData->loaded_partitions)[i]));
|
||||
ss << ((i == slotData->num_loaded_partitions- 1) ? "" : ",");
|
||||
}
|
||||
ss << "],";
|
||||
|
||||
ss << "\"cmdline\":\"" << slotData->cmdline << "\",";
|
||||
|
||||
ss << "\"rollback_indexes\":[";
|
||||
for (int i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
|
||||
ss << (slotData->rollback_indexes)[i];
|
||||
ss << ((i == AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS - 1) ? "" : ",");
|
||||
}
|
||||
ss << "],";
|
||||
|
||||
ss << "\"resolved_hashtree_error_mode\":\"" << toString(slotData->resolved_hashtree_error_mode) << "\"";
|
||||
ss << "}";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string toString(AvbHashtreeErrorMode errorMode) {
|
||||
static const char* AvbHashtreeErrorMode_STRING[5] = {
|
||||
"AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE",
|
||||
"AVB_HASHTREE_ERROR_MODE_RESTART",
|
||||
"AVB_HASHTREE_ERROR_MODE_EIO",
|
||||
"AVB_HASHTREE_ERROR_MODE_LOGGING",
|
||||
"AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO",
|
||||
};
|
||||
return AvbHashtreeErrorMode_STRING[errorMode];
|
||||
}
|
||||
|
||||
std::string toString(AvbSlotVerifyResult slotVerifyResult) {
|
||||
static const char* AvbSlotVerifyResult_STRING[9] = {
|
||||
"AVB_SLOT_VERIFY_RESULT_OK",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_OOM",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_IO",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION",
|
||||
"AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT",
|
||||
};
|
||||
return AvbSlotVerifyResult_STRING[slotVerifyResult];
|
||||
}
|
||||
|
||||
std::string toString(const uint8_t* ba, int baSize) {
|
||||
//sb.append(Integer.toString((inData[i].toInt().and(0xff)) + 0x100, 16).substring(1))
|
||||
char byteStr[8] = { 0 };
|
||||
std::stringstream ss;
|
||||
for (int i = 0; i < baSize; i++) {
|
||||
sprintf(byteStr, "%02x", ba[i]);
|
||||
ss << byteStr;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string toString(const AvbVBMetaData* vbmetaData) {
|
||||
std::stringstream ss;
|
||||
ss << "{";
|
||||
ss << "\"_type\":\"AvbVBMetaData\",";
|
||||
ss << "\"partition_name\":\"" << vbmetaData->partition_name << "\",";
|
||||
ss << "\"vbmeta_data\":\"" << toString((vbmetaData->vbmeta_data), vbmetaData->vbmeta_size) << "\",";
|
||||
ss << "\"vbmeta_size\":" << vbmetaData->vbmeta_size << ",";
|
||||
ss << "\"verify_result\":\"" << toString(vbmetaData->verify_result) << "\"";
|
||||
ss << "}";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string toString(/* enum */ AvbVBMetaVerifyResult metaVerifyResult) {
|
||||
static const char* AvbVBMetaVerifyResult_STRING[6] = {
|
||||
"AVB_VBMETA_VERIFY_RESULT_OK",
|
||||
"AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED",
|
||||
"AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER",
|
||||
"AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION",
|
||||
"AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH",
|
||||
"AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH",
|
||||
};
|
||||
return AvbVBMetaVerifyResult_STRING[metaVerifyResult];
|
||||
}
|
||||
|
||||
std::string toString(const AvbPartitionData* partitionData) {
|
||||
std::stringstream ss;
|
||||
ss << "{";
|
||||
ss << "\"partition_name\":\"" << partitionData->partition_name << "\",";
|
||||
//ss << "\"data\":\"" << toString(partitionData->data, partitionData->data_size) << "\",";
|
||||
ss << "\"data\":\"" << "omitted" << "\",";
|
||||
ss << "\"data_size\":\"" << partitionData->data_size << "\",";
|
||||
ss << "\"preloaded\":\"" << (partitionData->preloaded ? "true" : "false") << "\"";
|
||||
ss << "}";
|
||||
return ss.str();
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* helper.h
|
||||
* Copyright (C) 2020 yu <yu@X.local>
|
||||
*
|
||||
* Distributed under terms of the MIT license.
|
||||
*/
|
||||
|
||||
#ifndef HELPER_H
|
||||
#define HELPER_H
|
||||
|
||||
#include <string>
|
||||
#include <libavb.h>
|
||||
|
||||
std::string toString(const AvbSlotVerifyData* slotData);
|
||||
std::string toString(const AvbVBMetaData* vbmetaData);
|
||||
std::string toString(const AvbPartitionData* partitionData);
|
||||
std::string toString(const uint8_t* ba, int baSize);
|
||||
std::string toString(/* enum */ AvbHashtreeErrorMode errorMode);
|
||||
std::string toString(/* enum */ AvbSlotVerifyResult slotVerifyResult);
|
||||
std::string toString(/* enum */ AvbVBMetaVerifyResult metaVerifyResult);
|
||||
|
||||
#endif /* !HELPER_H */
|
Loading…
Reference in New Issue