1 /*
2  * Copyright (C) 2019 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 "writer.h"
18 
19 #include <android-base/file.h>
20 
21 #include "utility.h"
22 
23 using android::base::ErrnoError;
24 using android::base::Result;
25 
26 namespace android {
27 namespace fs_mgr {
28 
SerializeVBMetaTable(const VBMetaTable & input)29 std::string SerializeVBMetaTable(const VBMetaTable& input) {
30     std::string table;
31     table.append(reinterpret_cast<const char*>(&input.header), SUPER_VBMETA_HEADER_SIZE);
32 
33     for (const auto& desc : input.descriptors) {
34         table.append(reinterpret_cast<const char*>(&desc), SUPER_VBMETA_DESCRIPTOR_SIZE);
35         table.append(desc.vbmeta_name);
36     }
37 
38     // Ensure the size of vbmeta table is SUPER_VBMETA_TABLE_MAX_SIZE
39     table.resize(SUPER_VBMETA_TABLE_MAX_SIZE, '\0');
40 
41     return table;
42 }
43 
WritePrimaryVBMetaTable(int fd,const std::string & table)44 Result<void> WritePrimaryVBMetaTable(int fd, const std::string& table) {
45     const uint64_t offset = PRIMARY_SUPER_VBMETA_TABLE_OFFSET;
46     if (lseek(fd, offset, SEEK_SET) < 0) {
47         return ErrnoError() << __PRETTY_FUNCTION__ << " lseek failed";
48     }
49 
50     if (!android::base::WriteFully(fd, table.data(), table.size())) {
51         return ErrnoError() << "Failed to write primary vbmeta table at offset " << offset;
52     }
53     return {};
54 }
55 
WriteBackupVBMetaTable(int fd,const std::string & table)56 Result<void> WriteBackupVBMetaTable(int fd, const std::string& table) {
57     const uint64_t offset = BACKUP_SUPER_VBMETA_TABLE_OFFSET;
58     if (lseek(fd, offset, SEEK_SET) < 0) {
59         return ErrnoError() << __PRETTY_FUNCTION__ << " lseek failed";
60     }
61 
62     if (!android::base::WriteFully(fd, table.data(), table.size())) {
63         return ErrnoError() << "Failed to write backup vbmeta table at offset " << offset;
64     }
65     return {};
66 }
67 
WriteVBMetaImage(int fd,const uint8_t slot_number,const std::string & vbmeta_image)68 Result<void> WriteVBMetaImage(int fd, const uint8_t slot_number, const std::string& vbmeta_image) {
69     const uint64_t offset = IndexOffset(slot_number);
70     if (lseek(fd, offset, SEEK_SET) < 0) {
71         return ErrnoError() << __PRETTY_FUNCTION__ << " lseek failed";
72     }
73 
74     if (!android::base::WriteFully(fd, vbmeta_image.data(), vbmeta_image.size())) {
75         return ErrnoError() << "Failed to write vbmeta image at offset " << offset;
76     }
77     return {};
78 }
79 
80 }  // namespace fs_mgr
81 }  // namespace android