summaryrefslogtreecommitdiffstats
path: root/fs_mgr
diff options
context:
space:
mode:
authorDavid Anderson2018-07-13 16:25:39 -0500
committerDavid Anderson2018-07-13 20:17:56 -0500
commit62cd1e06abdb10ad8da6125083f6671639d7ba46 (patch)
tree178b19d36d797bb8d20babaae05df3fcff6420db /fs_mgr
parent8cf1e38b21b0c4206fc404742fb78429252baadc (diff)
downloadplatform-system-core-62cd1e06abdb10ad8da6125083f6671639d7ba46.tar.gz
platform-system-core-62cd1e06abdb10ad8da6125083f6671639d7ba46.tar.xz
platform-system-core-62cd1e06abdb10ad8da6125083f6671639d7ba46.zip
liblp: Move image reading/writing functions to a new cpp file.
This is in preparation for adding sparse image support, to avoid cluttering the more critical reading and writing logic. Bug: 79173901 Test: N/A Change-Id: Icdddb849aebba4adf18a3e63ffbd3f36adda812d
Diffstat (limited to 'fs_mgr')
-rw-r--r--fs_mgr/liblp/Android.bp1
-rw-r--r--fs_mgr/liblp/images.cpp79
-rw-r--r--fs_mgr/liblp/images.h29
-rw-r--r--fs_mgr/liblp/io_test.cpp1
-rw-r--r--fs_mgr/liblp/reader.cpp28
-rw-r--r--fs_mgr/liblp/reader.h3
-rw-r--r--fs_mgr/liblp/writer.cpp27
-rw-r--r--fs_mgr/liblp/writer.h8
8 files changed, 117 insertions, 59 deletions
diff --git a/fs_mgr/liblp/Android.bp b/fs_mgr/liblp/Android.bp
index 1050cf560..176671a59 100644
--- a/fs_mgr/liblp/Android.bp
+++ b/fs_mgr/liblp/Android.bp
@@ -24,6 +24,7 @@ cc_library_static {
24 ], 24 ],
25 srcs: [ 25 srcs: [
26 "builder.cpp", 26 "builder.cpp",
27 "images.cpp",
27 "reader.cpp", 28 "reader.cpp",
28 "utility.cpp", 29 "utility.cpp",
29 "writer.cpp", 30 "writer.cpp",
diff --git a/fs_mgr/liblp/images.cpp b/fs_mgr/liblp/images.cpp
new file mode 100644
index 000000000..c8d9f13d8
--- /dev/null
+++ b/fs_mgr/liblp/images.cpp
@@ -0,0 +1,79 @@
1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "images.h"
18
19#include <android-base/file.h>
20#include <android-base/unique_fd.h>
21
22#include "reader.h"
23#include "utility.h"
24#include "writer.h"
25
26namespace android {
27namespace fs_mgr {
28
29std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
30 LpMetadataGeometry geometry;
31 if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
32 return nullptr;
33 }
34 if (SeekFile64(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET) < 0) {
35 PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
36 return nullptr;
37 }
38 std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
39 if (!metadata) {
40 return nullptr;
41 }
42 metadata->geometry = geometry;
43 return metadata;
44}
45
46std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
47 android::base::unique_fd fd(open(file, O_RDONLY));
48 if (fd < 0) {
49 PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
50 return nullptr;
51 }
52 return ReadFromImageFile(fd);
53}
54
55bool WriteToImageFile(int fd, const LpMetadata& input) {
56 std::string geometry = SerializeGeometry(input.geometry);
57 std::string padding(LP_METADATA_GEOMETRY_SIZE - geometry.size(), '\0');
58 std::string metadata = SerializeMetadata(input);
59
60 std::string everything = geometry + padding + metadata;
61
62 if (!android::base::WriteFully(fd, everything.data(), everything.size())) {
63 PERROR << __PRETTY_FUNCTION__ << "write " << everything.size() << " bytes failed";
64 return false;
65 }
66 return true;
67}
68
69bool WriteToImageFile(const char* file, const LpMetadata& input) {
70 android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
71 if (fd < 0) {
72 PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
73 return false;
74 }
75 return WriteToImageFile(fd, input);
76}
77
78} // namespace fs_mgr
79} // namespace android
diff --git a/fs_mgr/liblp/images.h b/fs_mgr/liblp/images.h
new file mode 100644
index 000000000..3a999b89c
--- /dev/null
+++ b/fs_mgr/liblp/images.h
@@ -0,0 +1,29 @@
1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <liblp/liblp.h>
18
19namespace android {
20namespace fs_mgr {
21
22// Helper function to serialize geometry and metadata to a normal file, for
23// flashing or debugging.
24std::unique_ptr<LpMetadata> ReadFromImageFile(int fd);
25bool WriteToImageFile(const char* file, const LpMetadata& metadata);
26bool WriteToImageFile(int fd, const LpMetadata& metadata);
27
28} // namespace fs_mgr
29} // namespace android
diff --git a/fs_mgr/liblp/io_test.cpp b/fs_mgr/liblp/io_test.cpp
index e91cc3e0a..bbbedc761 100644
--- a/fs_mgr/liblp/io_test.cpp
+++ b/fs_mgr/liblp/io_test.cpp
@@ -24,6 +24,7 @@
24#include <gtest/gtest.h> 24#include <gtest/gtest.h>
25#include <liblp/builder.h> 25#include <liblp/builder.h>
26 26
27#include "images.h"
27#include "reader.h" 28#include "reader.h"
28#include "utility.h" 29#include "utility.h"
29#include "writer.h" 30#include "writer.h"
diff --git a/fs_mgr/liblp/reader.cpp b/fs_mgr/liblp/reader.cpp
index 7fbf8e6c0..117da5991 100644
--- a/fs_mgr/liblp/reader.cpp
+++ b/fs_mgr/liblp/reader.cpp
@@ -171,7 +171,7 @@ static bool ValidateMetadataHeader(const LpMetadataHeader& header) {
171 171
172// Parse and validate all metadata at the current position in the given file 172// Parse and validate all metadata at the current position in the given file
173// descriptor. 173// descriptor.
174static std::unique_ptr<LpMetadata> ParseMetadata(int fd) { 174std::unique_ptr<LpMetadata> ParseMetadata(int fd) {
175 // First read and validate the header. 175 // First read and validate the header.
176 std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>(); 176 std::unique_ptr<LpMetadata> metadata = std::make_unique<LpMetadata>();
177 if (!android::base::ReadFully(fd, &metadata->header, sizeof(metadata->header))) { 177 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
286 return ReadMetadata(fd, slot_number); 286 return ReadMetadata(fd, slot_number);
287} 287}
288 288
289std::unique_ptr<LpMetadata> ReadFromImageFile(int fd) {
290 LpMetadataGeometry geometry;
291 if (!ReadLogicalPartitionGeometry(fd, &geometry)) {
292 return nullptr;
293 }
294 if (SeekFile64(fd, LP_METADATA_GEOMETRY_SIZE, SEEK_SET) < 0) {
295 PERROR << __PRETTY_FUNCTION__ << "lseek failed: offset " << LP_METADATA_GEOMETRY_SIZE;
296 return nullptr;
297 }
298 std::unique_ptr<LpMetadata> metadata = ParseMetadata(fd);
299 if (!metadata) {
300 return nullptr;
301 }
302 metadata->geometry = geometry;
303 return metadata;
304}
305
306std::unique_ptr<LpMetadata> ReadFromImageFile(const char* file) {
307 android::base::unique_fd fd(open(file, O_RDONLY));
308 if (fd < 0) {
309 PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
310 return nullptr;
311 }
312 return ReadFromImageFile(fd);
313}
314
315static std::string NameFromFixedArray(const char* name, size_t buffer_size) { 289static std::string NameFromFixedArray(const char* name, size_t buffer_size) {
316 // If the end of the buffer has a null character, it's safe to assume the 290 // If the end of the buffer has a null character, it's safe to assume the
317 // buffer is null terminated. Otherwise, we cap the string to the input 291 // buffer is null terminated. Otherwise, we cap the string to the input
diff --git a/fs_mgr/liblp/reader.h b/fs_mgr/liblp/reader.h
index c4cac8fcb..843b2f25b 100644
--- a/fs_mgr/liblp/reader.h
+++ b/fs_mgr/liblp/reader.h
@@ -30,6 +30,7 @@ std::unique_ptr<LpMetadata> ReadMetadata(int fd, uint32_t slot_number);
30 30
31// Helper functions for manually reading geometry and metadata. 31// Helper functions for manually reading geometry and metadata.
32bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry); 32bool ReadLogicalPartitionGeometry(int fd, LpMetadataGeometry* geometry);
33std::unique_ptr<LpMetadata> ParseMetadata(int fd);
33 34
34// These functions assume a valid geometry and slot number. 35// These functions assume a valid geometry and slot number.
35std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry, 36std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry& geometry,
@@ -37,8 +38,6 @@ std::unique_ptr<LpMetadata> ReadPrimaryMetadata(int fd, const LpMetadataGeometry
37std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry, 38std::unique_ptr<LpMetadata> ReadBackupMetadata(int fd, const LpMetadataGeometry& geometry,
38 uint32_t slot_number); 39 uint32_t slot_number);
39 40
40std::unique_ptr<LpMetadata> ReadFromImageFile(int fd);
41
42} // namespace fs_mgr 41} // namespace fs_mgr
43} // namespace android 42} // namespace android
44 43
diff --git a/fs_mgr/liblp/writer.cpp b/fs_mgr/liblp/writer.cpp
index 3ec76a970..74c03bf2f 100644
--- a/fs_mgr/liblp/writer.cpp
+++ b/fs_mgr/liblp/writer.cpp
@@ -30,7 +30,7 @@
30namespace android { 30namespace android {
31namespace fs_mgr { 31namespace fs_mgr {
32 32
33static std::string SerializeGeometry(const LpMetadataGeometry& input) { 33std::string SerializeGeometry(const LpMetadataGeometry& input) {
34 LpMetadataGeometry geometry = input; 34 LpMetadataGeometry geometry = input;
35 memset(geometry.checksum, 0, sizeof(geometry.checksum)); 35 memset(geometry.checksum, 0, sizeof(geometry.checksum));
36 SHA256(&geometry, sizeof(geometry), geometry.checksum); 36 SHA256(&geometry, sizeof(geometry), geometry.checksum);
@@ -44,7 +44,7 @@ static bool CompareGeometry(const LpMetadataGeometry& g1, const LpMetadataGeomet
44 g1.last_logical_sector == g2.last_logical_sector; 44 g1.last_logical_sector == g2.last_logical_sector;
45} 45}
46 46
47static std::string SerializeMetadata(const LpMetadata& input) { 47std::string SerializeMetadata(const LpMetadata& input) {
48 LpMetadata metadata = input; 48 LpMetadata metadata = input;
49 LpMetadataHeader& header = metadata.header; 49 LpMetadataHeader& header = metadata.header;
50 50
@@ -318,28 +318,5 @@ bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_numb
318 return UpdatePartitionTable(fd, metadata, slot_number, DefaultWriter); 318 return UpdatePartitionTable(fd, metadata, slot_number, DefaultWriter);
319} 319}
320 320
321bool WriteToImageFile(int fd, const LpMetadata& input) {
322 std::string geometry = SerializeGeometry(input.geometry);
323 std::string padding(LP_METADATA_GEOMETRY_SIZE - geometry.size(), '\0');
324 std::string metadata = SerializeMetadata(input);
325
326 std::string everything = geometry + padding + metadata;
327
328 if (!android::base::WriteFully(fd, everything.data(), everything.size())) {
329 PERROR << __PRETTY_FUNCTION__ << "write " << everything.size() << " bytes failed";
330 return false;
331 }
332 return true;
333}
334
335bool WriteToImageFile(const char* file, const LpMetadata& input) {
336 android::base::unique_fd fd(open(file, O_CREAT | O_RDWR | O_TRUNC, 0644));
337 if (fd < 0) {
338 PERROR << __PRETTY_FUNCTION__ << "open failed: " << file;
339 return false;
340 }
341 return WriteToImageFile(fd, input);
342}
343
344} // namespace fs_mgr 321} // namespace fs_mgr
345} // namespace android 322} // namespace android
diff --git a/fs_mgr/liblp/writer.h b/fs_mgr/liblp/writer.h
index 94c1d3195..adbbebf66 100644
--- a/fs_mgr/liblp/writer.h
+++ b/fs_mgr/liblp/writer.h
@@ -25,6 +25,9 @@
25namespace android { 25namespace android {
26namespace fs_mgr { 26namespace fs_mgr {
27 27
28std::string SerializeGeometry(const LpMetadataGeometry& input);
29std::string SerializeMetadata(const LpMetadata& input);
30
28// These variants are for testing only. The path-based functions should be used 31// These variants are for testing only. The path-based functions should be used
29// for actual operation, so that open() is called with the correct flags. 32// for actual operation, so that open() is called with the correct flags.
30bool FlashPartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number); 33bool 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
33bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number, 36bool UpdatePartitionTable(int fd, const LpMetadata& metadata, uint32_t slot_number,
34 const std::function<bool(int, const std::string&)>& writer); 37 const std::function<bool(int, const std::string&)>& writer);
35 38
36// Helper function to serialize geometry and metadata to a normal file, for
37// flashing or debugging.
38bool WriteToImageFile(const char* file, const LpMetadata& metadata);
39bool WriteToImageFile(int fd, const LpMetadata& metadata);
40
41} // namespace fs_mgr 39} // namespace fs_mgr
42} // namespace android 40} // namespace android
43 41