Merge "liblp: Move image reading/writing functions to a new cpp file."
This commit is contained in:
commit
fcb0ef9c98
8 changed files with 117 additions and 59 deletions
|
|
@ -24,6 +24,7 @@ cc_library_static {
|
|||
],
|
||||
srcs: [
|
||||
"builder.cpp",
|
||||
"images.cpp",
|
||||
"reader.cpp",
|
||||
"utility.cpp",
|
||||
"writer.cpp",
|
||||
|
|
|
|||
79
fs_mgr/liblp/images.cpp
Normal file
79
fs_mgr/liblp/images.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "images.h"
|
||||
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
|
||||
#include "reader.h"
|
||||
#include "utility.h"
|
||||
#include "writer.h"
|
||||
|
||||
namespace android {
|
||||
namespace fs_mgr {
|
||||
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
|
||||
LpMetadataGeometry geometry;
|
||||
if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (SeekFile64(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET) < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
|
||||
if (!metadata) {
|
||||
return nullptr;
|
||||
}
|
||||
metadata->geometry = geometry;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
|
||||
android::base::unique_fd fd(open(file, O_RDONLY));
|
||||
if (fd < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
|
||||
return nullptr;
|
||||
}
|
||||
return ReadFromImageFile(fd);
|
||||
}
|
||||
|
||||
bool WriteToImageFile(int fd, const LpMetadata& input) {
|
||||
std::string geometry = SerializeGeometry(input.geometry);
|
||||
std::string padding(LP_METADATA_GEOMETRY_SIZE - geometry.size(), '\0');
|
||||
std::string metadata = SerializeMetadata(input);
|
||||
|
||||
std::string everything = geometry + padding + metadata;
|
||||
|
||||
if (!android::base::WriteFully(fd, everything.data(), everything.size())) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "write " << everything.size() << " bytes failed";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteToImageFile(const char* file, const LpMetadata& input) {
|
||||
android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
|
||||
if (fd < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
|
||||
return false;
|
||||
}
|
||||
return WriteToImageFile(fd, input);
|
||||
}
|
||||
|
||||
} // namespace fs_mgr
|
||||
} // namespace android
|
||||
29
fs_mgr/liblp/images.h
Normal file
29
fs_mgr/liblp/images.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <liblp/liblp.h>
|
||||
|
||||
namespace android {
|
||||
namespace fs_mgr {
|
||||
|
||||
// Helper function to serialize geometry and metadata to a normal file, for
|
||||
// flashing or debugging.
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(int fd);
|
||||
bool WriteToImageFile(const char* file, const LpMetadata& metadata);
|
||||
bool WriteToImageFile(int fd, const LpMetadata& metadata);
|
||||
|
||||
} // namespace fs_mgr
|
||||
} // namespace android
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <liblp/builder.h>
|
||||
|
||||
#include "images.h"
|
||||
#include "reader.h"
|
||||
#include "utility.h"
|
||||
#include "writer.h"
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@ static bool ValidateMetadataHeader(const LpMetadataHeader& header) {
|
|||
|
||||
// Parse and validate all metadata at the current position in the given file
|
||||
// descriptor.
|
||||
static std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
|
||||
std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
|
||||
// First read and validate the header.
|
||||
std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
|
||||
if (!android::base::ReadFully(fd, &metadata->header, sizeof(metadata->header))) {
|
||||
|
|
@ -286,32 +286,6 @@ std::unique_ptr<LpMetadata> ReadMetadata(const char* block_device, uint32_t slot
|
|||
return ReadMetadata(fd, slot_number);
|
||||
}
|
||||
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
|
||||
LpMetadataGeometry geometry;
|
||||
if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (SeekFile64(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET) < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
|
||||
if (!metadata) {
|
||||
return nullptr;
|
||||
}
|
||||
metadata->geometry = geometry;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
|
||||
android::base::unique_fd fd(open(file, O_RDONLY));
|
||||
if (fd < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
|
||||
return nullptr;
|
||||
}
|
||||
return ReadFromImageFile(fd);
|
||||
}
|
||||
|
||||
static std::string NameFromFixedArray(const char* name, size_t buffer_size) {
|
||||
// If the end of the buffer has a null character, it's safe to assume the
|
||||
// buffer is null terminated. Otherwise, we cap the string to the input
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number);
|
|||
|
||||
// Helper functions for manually reading geometry and metadata.
|
||||
bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry);
|
||||
std::unique_ptr<LpMetadata> ParseMetadata(int fd);
|
||||
|
||||
// These functions assume a valid geometry and slot number.
|
||||
std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,
|
||||
|
|
@ -37,8 +38,6 @@ std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry
|
|||
std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry,
|
||||
uint32_t slot_number);
|
||||
|
||||
std::unique_ptr<LpMetadata> ReadFromImageFile(int fd);
|
||||
|
||||
} // namespace fs_mgr
|
||||
} // namespace android
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
namespace android {
|
||||
namespace fs_mgr {
|
||||
|
||||
static std::string SerializeGeometry(const LpMetadataGeometry& input) {
|
||||
std::string SerializeGeometry(const LpMetadataGeometry& input) {
|
||||
LpMetadataGeometry geometry = input;
|
||||
memset(geometry.checksum, 0, sizeof(geometry.checksum));
|
||||
SHA256(&geometry, sizeof(geometry), geometry.checksum);
|
||||
|
|
@ -44,7 +44,7 @@ static bool CompareGeometry(const LpMetadataGeometry& g1, const LpMetadataGeomet
|
|||
g1.last_logical_sector == g2.last_logical_sector;
|
||||
}
|
||||
|
||||
static std::string SerializeMetadata(const LpMetadata& input) {
|
||||
std::string SerializeMetadata(const LpMetadata& input) {
|
||||
LpMetadata metadata = input;
|
||||
LpMetadataHeader& header = metadata.header;
|
||||
|
||||
|
|
@ -318,28 +318,5 @@ bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_numb
|
|||
return UpdatePartitionTable(fd, metadata, slot_number, DefaultWriter);
|
||||
}
|
||||
|
||||
bool WriteToImageFile(int fd, const LpMetadata& input) {
|
||||
std::string geometry = SerializeGeometry(input.geometry);
|
||||
std::string padding(LP_METADATA_GEOMETRY_SIZE - geometry.size(), '\0');
|
||||
std::string metadata = SerializeMetadata(input);
|
||||
|
||||
std::string everything = geometry + padding + metadata;
|
||||
|
||||
if (!android::base::WriteFully(fd, everything.data(), everything.size())) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "write " << everything.size() << " bytes failed";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteToImageFile(const char* file, const LpMetadata& input) {
|
||||
android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
|
||||
if (fd < 0) {
|
||||
PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
|
||||
return false;
|
||||
}
|
||||
return WriteToImageFile(fd, input);
|
||||
}
|
||||
|
||||
} // namespace fs_mgr
|
||||
} // namespace android
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@
|
|||
namespace android {
|
||||
namespace fs_mgr {
|
||||
|
||||
std::string SerializeGeometry(const LpMetadataGeometry& input);
|
||||
std::string SerializeMetadata(const LpMetadata& input);
|
||||
|
||||
// These variants are for testing only. The path-based functions should be used
|
||||
// for actual operation, so that open() is called with the correct flags.
|
||||
bool FlashPartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number);
|
||||
|
|
@ -33,11 +36,6 @@ bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_numb
|
|||
bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number,
|
||||
const std::function<bool(int, const std::string&)>& writer);
|
||||
|
||||
// Helper function to serialize geometry and metadata to a normal file, for
|
||||
// flashing or debugging.
|
||||
bool WriteToImageFile(const char* file, const LpMetadata& metadata);
|
||||
bool WriteToImageFile(int fd, const LpMetadata& metadata);
|
||||
|
||||
} // namespace fs_mgr
|
||||
} // namespace android
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue