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 // Code here has been inspired by
17 // https://github.com/u-boot/u-boot/blob/master/tools/mkenvimage.c The bare
18 // minimum amount of functionality for our application is replicated.
19
20 #include <zlib.h>
21
22 #include <android-base/logging.h>
23 #include <android-base/strings.h>
24 #include <gflags/gflags.h>
25
26 #include "common/libs/fs/shared_buf.h"
27 #include "common/libs/fs/shared_fd.h"
28 #include "common/libs/utils/files.h"
29 #include "common/libs/utils/result.h"
30
31 #define PAD_VALUE (0xff)
32 #define CRC_SIZE (sizeof(uint32_t))
33
34 // One NULL needed at the end of the env.
35 #define NULL_PAD_LENGTH (1)
36
37 DEFINE_uint32(env_size, 4096, "file size of resulting env");
38 DEFINE_string(output_path, "", "output file path");
39 DEFINE_string(input_path, "", "input file path");
40 namespace cuttlefish {
41
42 static constexpr char kUsageMessage[] =
43 "<flags>\n"
44 "\n"
45 "env_size - length in bytes of the resulting env image. Defaults to 4kb.\n"
46 "input_path - path to input key value mapping as a text file\n"
47 "output_path - path to write resulting environment image including CRC "
48 "to\n";
49
MkenvimageSlimMain(int argc,char ** argv)50 Result<int> MkenvimageSlimMain(int argc, char** argv) {
51 ::android::base::InitLogging(argv, android::base::StderrLogger);
52 gflags::SetUsageMessage(kUsageMessage);
53 gflags::ParseCommandLineFlags(&argc, &argv, true);
54 CF_EXPECT(FLAGS_output_path != "", "Output env path isn't defined.");
55 CF_EXPECT(FLAGS_env_size != 0, "env size can't be 0.");
56 CF_EXPECT(!(FLAGS_env_size % 512), "env size must be multiple of 512.");
57
58 std::string env_readout = ReadFile(FLAGS_input_path);
59 CF_EXPECT(env_readout.length(), "Input env is empty");
60 CF_EXPECT(
61 env_readout.length() <= (FLAGS_env_size - CRC_SIZE - NULL_PAD_LENGTH),
62 "Input env must fit within env_size specified.");
63
64 std::vector<uint8_t> env_buffer(FLAGS_env_size, PAD_VALUE);
65 uint8_t* env_ptr = env_buffer.data() + CRC_SIZE;
66 memcpy(env_ptr, env_readout.c_str(), FileSize(FLAGS_input_path));
67 env_ptr[env_readout.length()] = 0; // final byte after the env must be NULL
68 uint32_t crc = crc32(0, env_ptr, FLAGS_env_size - CRC_SIZE);
69 memcpy(env_buffer.data(), &crc, sizeof(uint32_t));
70
71 auto output_fd =
72 SharedFD::Creat(FLAGS_output_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
73 if (!output_fd->IsOpen()) {
74 return CF_ERR("Couldn't open the output file " + FLAGS_output_path);
75 } else if (FLAGS_env_size !=
76 WriteAll(output_fd, (char*)env_buffer.data(), FLAGS_env_size)) {
77 RemoveFile(FLAGS_output_path);
78 return CF_ERR("Couldn't complete write to " + FLAGS_output_path);
79 }
80
81 return 0;
82 }
83 } // namespace cuttlefish
84
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86 auto res = cuttlefish::MkenvimageSlimMain(argc, argv);
87 if (res.ok()) {
88 return *res;
89 }
90 LOG(ERROR) << "mkenvimage_slim failed: \n" << res.error().FormatForEnv();
91 abort();
92 }
93