blob: bf589d31f2060a7be5727abc7d5e9775dc7b368b [file] [log] [blame]
Tao Bao7ae01692019-05-16 14:42:42 -07001/*
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
Tianjie Xu3d57c842019-11-09 22:07:20 -080017#include "misc_writer/misc_writer.h"
Tao Bao7ae01692019-05-16 14:42:42 -070018
Tianjie Xu3d57c842019-11-09 22:07:20 -080019#include <string.h>
Tao Bao7ae01692019-05-16 14:42:42 -070020
Tianjie Xu3d57c842019-11-09 22:07:20 -080021#include <android-base/file.h>
Tao Bao7ae01692019-05-16 14:42:42 -070022#include <android-base/logging.h>
Tianjie Xu3d57c842019-11-09 22:07:20 -080023#include <android-base/stringprintf.h>
Tao Bao7ae01692019-05-16 14:42:42 -070024#include <bootloader_message/bootloader_message.h>
25
Tianjie Xu3d57c842019-11-09 22:07:20 -080026namespace android {
27namespace hardware {
28namespace google {
29namespace pixel {
Tao Bao7ae01692019-05-16 14:42:42 -070030
Tianjie Xu3d57c842019-11-09 22:07:20 -080031bool MiscWriter::OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
32 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
33 return size <= total_size && offset <= total_size - size;
Tao Bao7ae01692019-05-16 14:42:42 -070034}
35
Tianjie Xu3d57c842019-11-09 22:07:20 -080036bool MiscWriter::WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset,
37 std::string* err) {
38 if (!OffsetAndSizeInVendorSpace(offset, size)) {
39 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
40 return false;
41 }
42 auto misc_blk_device = get_misc_blk_device(err);
43 if (misc_blk_device.empty()) {
44 return false;
45 }
46 return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
47 err);
Tao Bao7ae01692019-05-16 14:42:42 -070048}
49
Tianjie Xu3d57c842019-11-09 22:07:20 -080050bool MiscWriter::PerformAction(std::optional<size_t> override_offset) {
Tao Bao7ae01692019-05-16 14:42:42 -070051 size_t offset = 0;
Tianjie Xu3d57c842019-11-09 22:07:20 -080052 std::string content;
53 switch (action_) {
54 case MiscWriterActions::kSetDarkThemeFlag:
55 case MiscWriterActions::kClearDarkThemeFlag:
56 offset = override_offset.value_or(kThemeFlagOffsetInVendorSpace);
57 content = (action_ == MiscWriterActions::kSetDarkThemeFlag)
58 ? kDarkThemeFlag
59 : std::string(strlen(kDarkThemeFlag), 0);
60 break;
61 case MiscWriterActions::kSetSotaFlag:
62 case MiscWriterActions::kClearSotaFlag:
63 offset = override_offset.value_or(kSotaFlagOffsetInVendorSpace);
64 content = (action_ == MiscWriterActions::kSetSotaFlag) ? kSotaFlag
65 : std::string(strlen(kSotaFlag), 0);
66 break;
67 case MiscWriterActions::kUnset:
68 LOG(ERROR) << "The misc writer action must be set";
69 return false;
Tao Bao7ae01692019-05-16 14:42:42 -070070 }
71
Tianjie Xu3d57c842019-11-09 22:07:20 -080072 if (std::string err;
73 !WriteMiscPartitionVendorSpace(content.data(), content.size(), offset, &err)) {
74 LOG(ERROR) << "Failed to write " << content << " at offset " << offset << " : " << err;
75 return false;
Tao Bao7ae01692019-05-16 14:42:42 -070076 }
Tianjie Xu3d57c842019-11-09 22:07:20 -080077 return true;
Tao Bao7ae01692019-05-16 14:42:42 -070078}
Tianjie Xu3d57c842019-11-09 22:07:20 -080079
80} // namespace pixel
81} // namespace google
82} // namespace hardware
83} // namespace android