diff options
author | Darren Krahn <dkrahn@google.com> | 2016-03-08 14:56:58 -0800 |
---|---|---|
committer | Darren Krahn <dkrahn@google.com> | 2016-03-10 13:02:55 -0800 |
commit | 088471151b7784cef5a8555e1f155454df106113 (patch) | |
tree | 7b445bfec0f9131bc304094c4c8bad94e283c7c8 /hal | |
parent | 811b136ab381658b7ec80aff885f80db495424d2 (diff) | |
download | nvram-088471151b7784cef5a8555e1f155454df106113.tar.gz |
Add support for get_max_space_size_in_bytes().
This method, added in v1.1 of the HAL, allows an implementation to
report the maximum supported size for a single space.
This CL also includes a few enhancements to improve testing and adds a
top level Android.mk for convenience.
BUG=27480753
Change-Id: I962cc7a31e869e72920cd80146b69974e50d5745
Diffstat (limited to 'hal')
-rw-r--r-- | hal/include/nvram/hal/tests/scoped_nvram_device.h | 1 | ||||
-rw-r--r-- | hal/nvram_device_adapter.cpp | 13 | ||||
-rw-r--r-- | hal/testing_nvram_implementation.cpp | 30 | ||||
-rw-r--r-- | hal/tests/nvram_hal_test.cc | 39 | ||||
-rw-r--r-- | hal/tests/scoped_nvram_device.cc | 14 |
5 files changed, 86 insertions, 11 deletions
diff --git a/hal/include/nvram/hal/tests/scoped_nvram_device.h b/hal/include/nvram/hal/tests/scoped_nvram_device.h index 14e31d1..7034a85 100644 --- a/hal/include/nvram/hal/tests/scoped_nvram_device.h +++ b/hal/include/nvram/hal/tests/scoped_nvram_device.h @@ -42,6 +42,7 @@ class ScopedNvramDevice { // Convenience methods which trivially wrap the device functions. virtual nvram_result_t GetTotalSizeInBytes(uint64_t* total_size); virtual nvram_result_t GetAvailableSizeInBytes(uint64_t* available_size); + virtual nvram_result_t GetMaxSpaceSizeInBytes(uint64_t* max_space_size); virtual nvram_result_t GetMaxSpaces(uint32_t* num_spaces); virtual nvram_result_t GetSpaceList(std::vector<uint32_t>* space_index_list); virtual nvram_result_t GetSpaceSize(uint32_t index, uint64_t* size); diff --git a/hal/nvram_device_adapter.cpp b/hal/nvram_device_adapter.cpp index fd23a1f..72f75f5 100644 --- a/hal/nvram_device_adapter.cpp +++ b/hal/nvram_device_adapter.cpp @@ -78,6 +78,16 @@ nvram_result_t device_get_available_size_in_bytes(const nvram_device_t* device, return result; } +nvram_result_t device_get_max_space_size_in_bytes(const nvram_device_t* device, + uint64_t* max_space_size) { + nvram::GetInfoRequest get_info_request; + nvram::GetInfoResponse get_info_response; + nvram_result_t result = Execute<nvram::COMMAND_GET_INFO>( + device, std::move(get_info_request), &get_info_response); + *max_space_size = get_info_response.max_space_size; + return result; +} + nvram_result_t device_get_max_spaces(const nvram_device_t* device, uint32_t* num_spaces) { nvram::GetInfoRequest get_info_request; @@ -292,12 +302,13 @@ NvramDeviceAdapter::NvramDeviceAdapter(const hw_module_t* module, memset(&device_, 0, sizeof(nvram_device_t)); device_.common.tag = HARDWARE_DEVICE_TAG; - device_.common.version = NVRAM_DEVICE_API_VERSION_0_1; + device_.common.version = NVRAM_DEVICE_API_VERSION_1_1; device_.common.module = const_cast<hw_module_t *>(module); device_.common.close = device_nvram_device_close; device_.get_total_size_in_bytes = device_get_total_size_in_bytes; device_.get_available_size_in_bytes = device_get_available_size_in_bytes; + device_.get_max_space_size_in_bytes = device_get_max_space_size_in_bytes; device_.get_max_spaces = device_get_max_spaces; device_.get_space_list = device_get_space_list; device_.get_space_size = device_get_space_size; diff --git a/hal/testing_nvram_implementation.cpp b/hal/testing_nvram_implementation.cpp index 81cbe36..81b49ae 100644 --- a/hal/testing_nvram_implementation.cpp +++ b/hal/testing_nvram_implementation.cpp @@ -35,10 +35,38 @@ class TestingNvramImplementation : public nvram::NvramImplementation { void Execute(const nvram::Request& request, nvram::Response* response) override { - nvram_manager_.Dispatch(request, response); + nvram::Blob request_blob; + if (!nvram::Encode(request, &request_blob)) { + response->result = NV_RESULT_INVALID_PARAMETER; + return; + } + nvram::Blob response_blob; + if (!ExecuteEncoded(request_blob, &response_blob)) { + response->result = NV_RESULT_INTERNAL_ERROR; + return; + } + if (!nvram::Decode(response_blob.data(), response_blob.size(), response)) { + response->result = NV_RESULT_INTERNAL_ERROR; + return; + } } private: + // Mixing encoding into the Execute flow emulates scenarios where requests are + // sent to another component for processing. + bool ExecuteEncoded(const nvram::Blob& request_blob, + nvram::Blob* response_blob) { + nvram::Request request; + if (!nvram::Decode(request_blob.data(), request_blob.size(), &request)) { + return false; + } + nvram::Response response; + nvram_manager_.Dispatch(request, &response); + if (!nvram::Encode(response, response_blob)) { + return false; + } + return true; + } nvram::NvramManager nvram_manager_; }; diff --git a/hal/tests/nvram_hal_test.cc b/hal/tests/nvram_hal_test.cc index edb7698..18373bb 100644 --- a/hal/tests/nvram_hal_test.cc +++ b/hal/tests/nvram_hal_test.cc @@ -216,6 +216,16 @@ TEST(NVRAMModuleTest, AvailableSize) { EXPECT_LE(available_size, total_size); } +TEST(NVRAMModuleTest, MaxSpaceSize) { + SafeScopedNvramDevice device; + uint64_t max_space_size = 0; + ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size)); + uint64_t total_size = 0; + ASSERT_EQ(NV_RESULT_SUCCESS, device.GetTotalSizeInBytes(&total_size)); + EXPECT_LE(max_space_size, total_size); + EXPECT_GE(max_space_size, 32u); +} + TEST(NVRAMModuleTest, MaxSpaces) { SafeScopedNvramDevice device; uint32_t num_spaces = 0; @@ -252,12 +262,12 @@ TEST(NVRAMModuleTest, SpaceList) { TEST(NVRAMModuleTest, SpaceSize) { SafeScopedNvramDevice device; ScopedNvramSpace space(&device, kTestIndex1, 17); - ScopedNvramSpace space2(&device, kTestIndex2, 1024); + ScopedNvramSpace space2(&device, kTestIndex2, 32); uint64_t size = 0; ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceSize(kTestIndex1, &size)); EXPECT_EQ(17u, size); ASSERT_EQ(NV_RESULT_SUCCESS, device.GetSpaceSize(kTestIndex2, &size)); - EXPECT_EQ(1024u, size); + EXPECT_EQ(32u, size); EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST, device.GetSpaceSize(kTestIndexNeverExists, &size)); } @@ -301,7 +311,12 @@ TEST(NVRAMModuleTest, CreateSmall) { TEST(NVRAMModuleTest, CreateLarge) { SafeScopedNvramDevice device; - ScopedNvramSpace space(&device, kTestIndex1, 256); + uint64_t max_space_size = 0; + ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size)); + uint64_t available_size = 0; + ASSERT_EQ(NV_RESULT_SUCCESS, device.GetAvailableSizeInBytes(&available_size)); + ScopedNvramSpace space(&device, kTestIndex1, + std::min(max_space_size, available_size)); } TEST(NVRAMModuleTest, CreateWithCustomControls) { @@ -489,9 +504,10 @@ TEST(NVRAMModuleTest, WriteExtend) { TEST(NVRAMModuleTest, WriteExtendTooShort) { uint32_t index = kTestIndex1; SafeScopedNvramDevice device; + // Only SHA-256 is supported. Try 20 which is SHA-1 output. EXPECT_EQ( NV_RESULT_INVALID_PARAMETER, - device.CreateSpace(index, 16, {NV_CONTROL_WRITE_EXTEND}, kNoAuth)); + device.CreateSpace(index, 20, {NV_CONTROL_WRITE_EXTEND}, kNoAuth)); EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST, device.WriteSpace(index, "test", kNoAuth)); } @@ -499,11 +515,16 @@ TEST(NVRAMModuleTest, WriteExtendTooShort) { TEST(NVRAMModuleTest, WriteExtendTooLong) { uint32_t index = kTestIndex1; SafeScopedNvramDevice device; - EXPECT_EQ( - NV_RESULT_INVALID_PARAMETER, - device.CreateSpace(index, 64, {NV_CONTROL_WRITE_EXTEND}, kNoAuth)); - EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST, - device.WriteSpace(index, "test", kNoAuth)); + uint64_t max_space_size = 0; + ASSERT_EQ(NV_RESULT_SUCCESS, device.GetMaxSpaceSizeInBytes(&max_space_size)); + if (max_space_size > 32) { + // Only SHA-256 is supported. Try 64 which is SHA-512 output. + EXPECT_EQ(NV_RESULT_INVALID_PARAMETER, + device.CreateSpace(index, std::min<uint64_t>(max_space_size, 64), + {NV_CONTROL_WRITE_EXTEND}, kNoAuth)); + EXPECT_EQ(NV_RESULT_SPACE_DOES_NOT_EXIST, + device.WriteSpace(index, "test", kNoAuth)); + } } TEST(NVRAMModuleTest, InitialValue) { diff --git a/hal/tests/scoped_nvram_device.cc b/hal/tests/scoped_nvram_device.cc index ba6dae5..3a001f3 100644 --- a/hal/tests/scoped_nvram_device.cc +++ b/hal/tests/scoped_nvram_device.cc @@ -47,6 +47,12 @@ ScopedNvramDevice::ScopedNvramDevice() { device_ = nullptr; return; } + if (device_->common.version != NVRAM_DEVICE_API_VERSION_1_1) { + LOG(ERROR) << "Unsupported NVRAM HAL version."; + nvram_close(device_); + device_ = nullptr; + return; + } } ScopedNvramDevice::~ScopedNvramDevice() { @@ -74,6 +80,14 @@ nvram_result_t ScopedNvramDevice::GetAvailableSizeInBytes( return device_->get_available_size_in_bytes(device_, available_size); } +nvram_result_t ScopedNvramDevice::GetMaxSpaceSizeInBytes( + uint64_t* max_space_size) { + if (!device_) { + return NV_RESULT_INTERNAL_ERROR; + } + return device_->get_max_space_size_in_bytes(device_, max_space_size); +} + nvram_result_t ScopedNvramDevice::GetMaxSpaces(uint32_t* num_spaces) { if (!device_) { return NV_RESULT_INTERNAL_ERROR; |