blob: 1d9702ebf81c1c924189126cb427ef44c118d16d [file] [log] [blame]
Tao Bao35e0f6d2019-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
17#include <errno.h>
18#include <getopt.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <unistd.h>
22
23#include <iostream>
24#include <string>
25#include <string_view>
26#include <vector>
27
28#include <android-base/logging.h>
29#include <android-base/parseint.h>
30#include <bootloader_message/bootloader_message.h>
31
32using namespace std::string_literals;
33
34static std::vector<uint8_t> ParseHexString(std::string_view hex_string) {
35 auto length = hex_string.size();
36 if (length % 2 != 0 || length == 0) {
37 return {};
38 }
39
40 std::vector<uint8_t> result(length / 2);
41 for (size_t i = 0; i < length / 2; i++) {
42 auto sub = "0x" + std::string(hex_string.substr(i * 2, 2));
43 if (!android::base::ParseUint(sub, &result[i])) {
44 return {};
45 }
46 }
47 return result;
48}
49
50static int Usage(std::string_view name) {
51 std::cerr << name << " usage:\n";
52 std::cerr << name << " [--vendor-space-offset <offset>] --hex-string 0xABCDEF\n";
53 std::cerr << "Writes the given hex string to the specified offset in vendor space in /misc "
54 "partition. Offset defaults to 0 if unspecified.\n";
55 return EXIT_FAILURE;
56}
57
58// misc_writer is a vendor tool that writes data to the vendor space in /misc.
59int main(int argc, char** argv) {
60 constexpr struct option OPTIONS[] = {
61 { "vendor-space-offset", required_argument, nullptr, 0 },
62 { "hex-string", required_argument, nullptr, 0 },
63 { nullptr, 0, nullptr, 0 },
64 };
65
66 // Offset defaults to 0 if unspecified.
67 size_t offset = 0;
68 std::string_view hex_string;
69
70 int arg;
71 int option_index;
72 while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
73 if (arg != 0) {
74 LOG(ERROR) << "Invalid command argument";
75 return Usage(argv[0]);
76 }
77 auto option_name = OPTIONS[option_index].name;
78 if (option_name == "vendor-space-offset"s) {
79 if (!android::base::ParseUint(optarg, &offset)) {
80 LOG(ERROR) << "Failed to parse the offset: " << optarg;
81 return Usage(argv[0]);
82 }
83 } else if (option_name == "hex-string"s) {
84 hex_string = optarg;
85 }
86 }
87
88 if (hex_string.starts_with("0x") || hex_string.starts_with("0X")) {
89 hex_string = hex_string.substr(2);
90 }
91 if (hex_string.empty()) {
92 LOG(ERROR) << "Invalid input hex string: " << hex_string;
93 return Usage(argv[0]);
94 }
95
96 auto data = ParseHexString(hex_string);
97 if (data.empty()) {
98 LOG(ERROR) << "Failed to parse the input hex string: " << hex_string;
99 return EXIT_FAILURE;
100 }
101 if (std::string err; !WriteMiscPartitionVendorSpace(data.data(), data.size(), offset, &err)) {
102 LOG(ERROR) << "Failed to write to misc partition: " << err;
103 return EXIT_FAILURE;
104 }
105 return EXIT_SUCCESS;
106}