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 "host/commands/assemble_cvd/boot_config.h"
18
19 #include <fstream>
20 #include <sstream>
21 #include <string>
22
23 #include <sys/stat.h>
24
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27
28 #include "common/libs/utils/files.h"
29 #include "common/libs/utils/subprocess.h"
30 #include "host/libs/config/cuttlefish_config.h"
31 #include "host/libs/config/kernel_args.h"
32
33 namespace {
34
WriteEnvironment(const cuttlefish::CuttlefishConfig & config,const std::string & env_path)35 size_t WriteEnvironment(const cuttlefish::CuttlefishConfig& config,
36 const std::string& env_path) {
37 std::ostringstream env;
38 auto kernel_args = KernelCommandLineFromConfig(config);
39 env << "bootargs=" << android::base::Join(kernel_args, " ") << '\0';
40 if (!config.boot_slot().empty()) {
41 env << "android_slot_suffix=_" << config.boot_slot() << '\0';
42 }
43 env << "bootdevice=0:1" << '\0';
44 env << "bootdelay=0" << '\0';
45 env << "bootcmd=boot_android virtio -" << '\0';
46 env << '\0';
47 std::string env_str = env.str();
48 std::ofstream file_out(env_path.c_str(), std::ios::binary);
49 file_out << env_str;
50
51 if(!file_out.good()) {
52 return 0;
53 }
54
55 return env_str.length();
56 }
57
58 } // namespace
59
60
InitBootloaderEnvPartition(const cuttlefish::CuttlefishConfig & config,const std::string & boot_env_image_path)61 bool InitBootloaderEnvPartition(const cuttlefish::CuttlefishConfig& config,
62 const std::string& boot_env_image_path) {
63 auto tmp_boot_env_image_path = boot_env_image_path + ".tmp";
64 auto uboot_env_path = config.AssemblyPath("u-boot.env");
65 if(!WriteEnvironment(config, uboot_env_path)) {
66 LOG(ERROR) << "Unable to write out plaintext env '" << uboot_env_path << ".'";
67 return false;
68 }
69
70 auto mkimage_path = cuttlefish::DefaultHostArtifactsPath("bin/mkenvimage");
71 cuttlefish::Command cmd(mkimage_path);
72 cmd.AddParameter("-s");
73 cmd.AddParameter("4096");
74 cmd.AddParameter("-o");
75 cmd.AddParameter(tmp_boot_env_image_path);
76 cmd.AddParameter(uboot_env_path);
77 int success = cmd.Start().Wait();
78 if (success != 0) {
79 LOG(ERROR) << "Unable to run mkenvimage. Exited with status " << success;
80 return false;
81 }
82
83 if(!cuttlefish::FileExists(boot_env_image_path) || cuttlefish::ReadFile(boot_env_image_path) != cuttlefish::ReadFile(tmp_boot_env_image_path)) {
84 if(!cuttlefish::RenameFile(tmp_boot_env_image_path, boot_env_image_path)) {
85 LOG(ERROR) << "Unable to delete the old env image.";
86 return false;
87 }
88 LOG(DEBUG) << "Updated bootloader environment image.";
89 } else {
90 cuttlefish::RemoveFile(tmp_boot_env_image_path);
91 }
92
93 return true;
94 }
95