1 /*
2 * Copyright (C) 2020 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_image_utils.h"
18
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <fstream>
23 #include <memory>
24 #include <regex>
25 #include <string>
26
27 #include <android-base/logging.h>
28 #include <android-base/strings.h>
29
30 #include "android-base/scopeguard.h"
31 #include "common/libs/fs/shared_fd.h"
32 #include "common/libs/utils/files.h"
33 #include "common/libs/utils/result.h"
34 #include "common/libs/utils/subprocess.h"
35 #include "host/libs/avb/avb.cpp"
36 #include "host/libs/config/config_utils.h"
37 #include "host/libs/config/cuttlefish_config.h"
38 #include "host/libs/config/known_paths.h"
39
40 namespace cuttlefish {
41 namespace {
42
43 constexpr char TMP_EXTENSION[] = ".tmp";
44 constexpr char kCpioExt[] = ".cpio";
45 constexpr char TMP_RD_DIR[] = "stripped_ramdisk_dir";
46 constexpr char STRIPPED_RD[] = "stripped_ramdisk";
47 constexpr char kConcatenatedVendorRamdisk[] = "concatenated_vendor_ramdisk";
48
RunMkBootFs(const std::string & input_dir,const std::string & output)49 void RunMkBootFs(const std::string& input_dir, const std::string& output) {
50 SharedFD output_fd = SharedFD::Open(output, O_CREAT | O_RDWR | O_TRUNC, 0644);
51 CHECK(output_fd->IsOpen()) << output_fd->StrError();
52
53 int success = Command(HostBinaryPath("mkbootfs"))
54 .AddParameter(input_dir)
55 .RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_fd)
56 .Start()
57 .Wait();
58 CHECK_EQ(success, 0) << "`mkbootfs` failed.";
59 }
60
RunLz4(const std::string & input,const std::string & output)61 void RunLz4(const std::string& input, const std::string& output) {
62 SharedFD output_fd = SharedFD::Open(output, O_CREAT | O_RDWR | O_TRUNC, 0644);
63 CHECK(output_fd->IsOpen()) << output_fd->StrError();
64 int success = Command(HostBinaryPath("lz4"))
65 .AddParameter("-c")
66 .AddParameter("-l")
67 .AddParameter("-12")
68 .AddParameter("--favor-decSpeed")
69 .AddParameter(input)
70 .RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_fd)
71 .Start()
72 .Wait();
73 CHECK_EQ(success, 0) << "`lz4` failed to transform '" << input << "' to '"
74 << output << "'";
75 }
76
ExtractValue(const std::string & dictionary,const std::string & key)77 std::string ExtractValue(const std::string& dictionary, const std::string& key) {
78 std::size_t index = dictionary.find(key);
79 if (index != std::string::npos) {
80 std::size_t end_index = dictionary.find('\n', index + key.length());
81 if (end_index != std::string::npos) {
82 return dictionary.substr(index + key.length(),
83 end_index - index - key.length());
84 }
85 }
86 return "";
87 }
88
89 // Though it is just as fast to overwrite the existing boot images with the newly generated ones,
90 // the cuttlefish composite disk generator checks the age of each of the components and
91 // regenerates the disk outright IF any one of the components is younger/newer than the current
92 // composite disk. If this file overwrite occurs, that condition is fulfilled. This action then
93 // causes data in the userdata partition from previous boots to be lost (which is not expected by
94 // the user if they've been booting the same kernel/ramdisk combination repeatedly).
95 // Consequently, the file is checked for differences and ONLY overwritten if there is a diff.
DeleteTmpFileIfNotChanged(const std::string & tmp_file,const std::string & current_file)96 bool DeleteTmpFileIfNotChanged(const std::string& tmp_file, const std::string& current_file) {
97 if (!FileExists(current_file) ||
98 ReadFile(current_file) != ReadFile(tmp_file)) {
99 if (!RenameFile(tmp_file, current_file).ok()) {
100 LOG(ERROR) << "Unable to delete " << current_file;
101 return false;
102 }
103 LOG(DEBUG) << "Updated " << current_file;
104 } else {
105 LOG(DEBUG) << "Didn't update " << current_file;
106 RemoveFile(tmp_file);
107 }
108
109 return true;
110 }
111
RepackVendorRamdisk(const std::string & kernel_modules_ramdisk_path,const std::string & original_ramdisk_path,const std::string & new_ramdisk_path,const std::string & build_dir)112 void RepackVendorRamdisk(const std::string& kernel_modules_ramdisk_path,
113 const std::string& original_ramdisk_path,
114 const std::string& new_ramdisk_path,
115 const std::string& build_dir) {
116 int success = 0;
117 const std::string ramdisk_stage_dir = build_dir + "/" + TMP_RD_DIR;
118 UnpackRamdisk(original_ramdisk_path, ramdisk_stage_dir);
119
120 success = Execute({"rm", "-rf", ramdisk_stage_dir + "/lib/modules"});
121 CHECK(success == 0) << "Could not rmdir \"lib/modules\" in TMP_RD_DIR. "
122 << "Exited with status " << success;
123
124 const std::string stripped_ramdisk_path = build_dir + "/" + STRIPPED_RD;
125
126 PackRamdisk(ramdisk_stage_dir, stripped_ramdisk_path);
127
128 // Concatenates the stripped ramdisk and input ramdisk and places the result at new_ramdisk_path
129 std::ofstream final_rd(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
130 std::ifstream ramdisk_a(stripped_ramdisk_path, std::ios_base::binary);
131 std::ifstream ramdisk_b(kernel_modules_ramdisk_path, std::ios_base::binary);
132 final_rd << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
133 }
134
IsCpioArchive(const std::string & path)135 bool IsCpioArchive(const std::string& path) {
136 static constexpr std::string_view CPIO_MAGIC = "070701";
137 auto fd = SharedFD::Open(path, O_RDONLY);
138 std::array<char, CPIO_MAGIC.size()> buf{};
139 if (fd->Read(buf.data(), buf.size()) != CPIO_MAGIC.size()) {
140 return false;
141 }
142 return memcmp(buf.data(), CPIO_MAGIC.data(), CPIO_MAGIC.size()) == 0;
143 }
144
145 } // namespace
146
PackRamdisk(const std::string & ramdisk_stage_dir,const std::string & output_ramdisk)147 void PackRamdisk(const std::string& ramdisk_stage_dir,
148 const std::string& output_ramdisk) {
149 RunMkBootFs(ramdisk_stage_dir, output_ramdisk + kCpioExt);
150 RunLz4(output_ramdisk + kCpioExt, output_ramdisk);
151 }
152
UnpackRamdisk(const std::string & original_ramdisk_path,const std::string & ramdisk_stage_dir)153 void UnpackRamdisk(const std::string& original_ramdisk_path,
154 const std::string& ramdisk_stage_dir) {
155 int success = 0;
156 if (IsCpioArchive(original_ramdisk_path)) {
157 CHECK(Copy(original_ramdisk_path, original_ramdisk_path + kCpioExt))
158 << "failed to copy " << original_ramdisk_path << " to "
159 << original_ramdisk_path + kCpioExt;
160 } else {
161 SharedFD output_fd = SharedFD::Open(original_ramdisk_path + kCpioExt,
162 O_CREAT | O_RDWR | O_TRUNC, 0644);
163 CHECK(output_fd->IsOpen()) << output_fd->StrError();
164
165 success = Command(HostBinaryPath("lz4"))
166 .AddParameter("-c")
167 .AddParameter("-d")
168 .AddParameter("-l")
169 .AddParameter(original_ramdisk_path)
170 .RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_fd)
171 .Start()
172 .Wait();
173 CHECK_EQ(success, 0) << "Unable to run lz4 on file '"
174 << original_ramdisk_path << "'.";
175 }
176 const auto ret = EnsureDirectoryExists(ramdisk_stage_dir);
177 CHECK(ret.ok()) << ret.error().FormatForEnv();
178
179 SharedFD input = SharedFD::Open(original_ramdisk_path + kCpioExt, O_RDONLY);
180 int cpio_status;
181 do {
182 LOG(ERROR) << "Running";
183 cpio_status = Command(HostBinaryPath("toybox"))
184 .AddParameter("cpio")
185 .AddParameter("-idu")
186 .SetWorkingDirectory(ramdisk_stage_dir)
187 .RedirectStdIO(Subprocess::StdIOChannel::kStdIn, input)
188 .Start()
189 .Wait();
190 } while (cpio_status == 0);
191 }
192
GetAvbMetadataFromBootImage(const std::string & boot_image_path,const std::string & unpack_dir)193 bool GetAvbMetadataFromBootImage(const std::string& boot_image_path,
194 const std::string& unpack_dir) {
195 std::unique_ptr<Avb> avbtool = GetDefaultAvb();
196 Result<void> result =
197 avbtool->WriteInfoImage(boot_image_path, unpack_dir + "/boot_params");
198 if (!result.ok()) {
199 LOG(ERROR) << result.error().Trace();
200 return false;
201 }
202 return true;
203 }
204
UnpackBootImage(const std::string & boot_image_path,const std::string & unpack_dir)205 Result<void> UnpackBootImage(const std::string& boot_image_path,
206 const std::string& unpack_dir) {
207 SharedFD output_file = SharedFD::Creat(unpack_dir + "/boot_params", 0666);
208 CF_EXPECTF(output_file->IsOpen(),
209 "Unable to create intermediate boot params file: '{}'",
210 output_file->StrError());
211
212 Command unpack_cmd =
213 Command(HostBinaryPath("unpack_bootimg"))
214 .AddParameter("--boot_img")
215 .AddParameter(boot_image_path)
216 .AddParameter("--out")
217 .AddParameter(unpack_dir)
218 .RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_file);
219
220 CF_EXPECT_EQ(unpack_cmd.Start().Wait(), 0, "Unable to run unpack_bootimg.");
221
222 return {};
223 }
224
UnpackVendorBootImageIfNotUnpacked(const std::string & vendor_boot_image_path,const std::string & unpack_dir)225 bool UnpackVendorBootImageIfNotUnpacked(
226 const std::string& vendor_boot_image_path, const std::string& unpack_dir) {
227 // the vendor boot params file is created during the first unpack. If it's
228 // already there, a unpack has occurred and there's no need to repeat the
229 // process.
230 if (FileExists(unpack_dir + "/vendor_boot_params")) {
231 return true;
232 }
233
234 auto unpack_path = HostBinaryPath("unpack_bootimg");
235 Command unpack_cmd(unpack_path);
236 unpack_cmd.AddParameter("--boot_img");
237 unpack_cmd.AddParameter(vendor_boot_image_path);
238 unpack_cmd.AddParameter("--out");
239 unpack_cmd.AddParameter(unpack_dir);
240 auto output_file = SharedFD::Creat(unpack_dir + "/vendor_boot_params", 0666);
241 if (!output_file->IsOpen()) {
242 LOG(ERROR) << "Unable to create intermediate vendor boot params file: "
243 << output_file->StrError();
244 return false;
245 }
246 unpack_cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut, output_file);
247 int success = unpack_cmd.Start().Wait();
248 if (success != 0) {
249 LOG(ERROR) << "Unable to run unpack_bootimg. Exited with status " << success;
250 return false;
251 }
252
253 // Concatenates all vendor ramdisk into one single ramdisk.
254 std::string concat_file_path = unpack_dir + "/" + kConcatenatedVendorRamdisk;
255 SharedFD concat_file = SharedFD::Creat(concat_file_path, 0666);
256 if (!concat_file->IsOpen()) {
257 LOG(ERROR) << "Unable to create concatenated vendor ramdisk file: "
258 << concat_file->StrError();
259 return false;
260 }
261
262 Result<std::vector<std::string>> unpack_files = DirectoryContents(unpack_dir);
263 if (!unpack_files.ok()) {
264 LOG(ERROR) << "No unpacked files: " << unpack_files.error().FormatForEnv();
265 return false;
266 }
267 for (const std::string& unpacked : *unpack_files) {
268 LOG(ERROR) << "acs: " << unpacked;
269 if (!android::base::StartsWith(unpacked, "vendor_ramdisk")) {
270 continue;
271 }
272 std::string input_path = unpack_dir + "/" + unpacked;
273 SharedFD input = SharedFD::Open(input_path, O_RDONLY);
274 if (!input->IsOpen()) {
275 LOG(ERROR) << "Failed to open '" << input_path << ": "
276 << input->StrError();
277 return false;
278 }
279 if (!concat_file->CopyAllFrom(*input)) {
280 LOG(ERROR) << "Failed to copy from '" << input_path << "' to '"
281 << concat_file_path << "'";
282 return false;
283 }
284 }
285 return true;
286 }
287
RepackBootImage(const Avb & avb,const std::string & new_kernel_path,const std::string & boot_image_path,const std::string & new_boot_image_path,const std::string & build_dir)288 Result<void> RepackBootImage(const Avb& avb,
289 const std::string& new_kernel_path,
290 const std::string& boot_image_path,
291 const std::string& new_boot_image_path,
292 const std::string& build_dir) {
293 CF_EXPECT(UnpackBootImage(boot_image_path, build_dir));
294
295 std::string boot_params = ReadFile(build_dir + "/boot_params");
296 auto kernel_cmdline = ExtractValue(boot_params, "command line args: ");
297 LOG(DEBUG) << "Cmdline from boot image is " << kernel_cmdline;
298
299 auto tmp_boot_image_path = new_boot_image_path + TMP_EXTENSION;
300 auto repack_path = HostBinaryPath("mkbootimg");
301 Command repack_cmd(repack_path);
302 repack_cmd.AddParameter("--kernel");
303 repack_cmd.AddParameter(new_kernel_path);
304 repack_cmd.AddParameter("--ramdisk");
305 repack_cmd.AddParameter(build_dir + "/ramdisk");
306 repack_cmd.AddParameter("--header_version");
307 repack_cmd.AddParameter("4");
308 repack_cmd.AddParameter("--cmdline");
309 repack_cmd.AddParameter(kernel_cmdline);
310 repack_cmd.AddParameter("-o");
311 repack_cmd.AddParameter(tmp_boot_image_path);
312 int result = repack_cmd.Start().Wait();
313 CF_EXPECT(result == 0, "Unable to run mkbootimg. Exited with status " << result);
314
315 CF_EXPECT(avb.AddHashFooter(tmp_boot_image_path, "boot", FileSize(boot_image_path)));
316 CF_EXPECT(DeleteTmpFileIfNotChanged(tmp_boot_image_path, new_boot_image_path));
317
318 return {};
319 }
320
RepackVendorBootImage(const std::string & new_ramdisk,const std::string & vendor_boot_image_path,const std::string & new_vendor_boot_image_path,const std::string & unpack_dir,bool bootconfig_supported)321 bool RepackVendorBootImage(const std::string& new_ramdisk,
322 const std::string& vendor_boot_image_path,
323 const std::string& new_vendor_boot_image_path,
324 const std::string& unpack_dir,
325 bool bootconfig_supported) {
326 if (UnpackVendorBootImageIfNotUnpacked(vendor_boot_image_path, unpack_dir) ==
327 false) {
328 return false;
329 }
330
331 std::string ramdisk_path;
332 if (new_ramdisk.size()) {
333 ramdisk_path = unpack_dir + "/vendor_ramdisk_repacked";
334 if (!FileExists(ramdisk_path)) {
335 RepackVendorRamdisk(new_ramdisk,
336 unpack_dir + "/" + kConcatenatedVendorRamdisk,
337 ramdisk_path, unpack_dir);
338 }
339 } else {
340 ramdisk_path = unpack_dir + "/" + kConcatenatedVendorRamdisk;
341 }
342
343 std::string bootconfig = ReadFile(unpack_dir + "/bootconfig");
344 LOG(DEBUG) << "Bootconfig parameters from vendor boot image are "
345 << bootconfig;
346 std::string vendor_boot_params = ReadFile(unpack_dir + "/vendor_boot_params");
347 auto kernel_cmdline =
348 ExtractValue(vendor_boot_params, "vendor command line args: ") +
349 (bootconfig_supported
350 ? ""
351 : " " + android::base::StringReplace(bootconfig, "\n", " ", true));
352 if (!bootconfig_supported) {
353 // TODO(b/182417593): Until we pass the module parameters through
354 // modules.options, we pass them through bootconfig using
355 // 'kernel.<key>=<value>' But if we don't support bootconfig, we need to
356 // rename them back to the old cmdline version
357 kernel_cmdline = android::base::StringReplace(
358 kernel_cmdline, " kernel.", " ", true);
359 }
360 LOG(DEBUG) << "Cmdline from vendor boot image is " << kernel_cmdline;
361
362 auto tmp_vendor_boot_image_path = new_vendor_boot_image_path + TMP_EXTENSION;
363 auto repack_path = HostBinaryPath("mkbootimg");
364 Command repack_cmd(repack_path);
365 repack_cmd.AddParameter("--vendor_ramdisk");
366 repack_cmd.AddParameter(ramdisk_path);
367 repack_cmd.AddParameter("--header_version");
368 repack_cmd.AddParameter("4");
369 repack_cmd.AddParameter("--vendor_cmdline");
370 repack_cmd.AddParameter(kernel_cmdline);
371 repack_cmd.AddParameter("--vendor_boot");
372 repack_cmd.AddParameter(tmp_vendor_boot_image_path);
373 repack_cmd.AddParameter("--dtb");
374 repack_cmd.AddParameter(unpack_dir + "/dtb");
375 if (bootconfig_supported) {
376 repack_cmd.AddParameter("--vendor_bootconfig");
377 repack_cmd.AddParameter(unpack_dir + "/bootconfig");
378 }
379
380 int success = repack_cmd.Start().Wait();
381 if (success != 0) {
382 LOG(ERROR) << "Unable to run mkbootimg. Exited with status " << success;
383 return false;
384 }
385
386 auto avbtool = Avb(AvbToolBinary());
387 Result<void> result =
388 avbtool.AddHashFooter(tmp_vendor_boot_image_path, "vendor_boot",
389 FileSize(vendor_boot_image_path));
390 if (!result.ok()) {
391 LOG(ERROR) << result.error().Trace();
392 return false;
393 }
394
395 return DeleteTmpFileIfNotChanged(tmp_vendor_boot_image_path, new_vendor_boot_image_path);
396 }
397
RepackVendorBootImageWithEmptyRamdisk(const std::string & vendor_boot_image_path,const std::string & new_vendor_boot_image_path,const std::string & unpack_dir,bool bootconfig_supported)398 bool RepackVendorBootImageWithEmptyRamdisk(
399 const std::string& vendor_boot_image_path,
400 const std::string& new_vendor_boot_image_path,
401 const std::string& unpack_dir, bool bootconfig_supported) {
402 auto empty_ramdisk_file =
403 SharedFD::Creat(unpack_dir + "/empty_ramdisk", 0666);
404 return RepackVendorBootImage(
405 unpack_dir + "/empty_ramdisk", vendor_boot_image_path,
406 new_vendor_boot_image_path, unpack_dir, bootconfig_supported);
407 }
408
RepackGem5BootImage(const std::string & initrd_path,const std::string & bootconfig_path,const std::string & unpack_dir,const std::string & input_ramdisk_path)409 void RepackGem5BootImage(const std::string& initrd_path,
410 const std::string& bootconfig_path,
411 const std::string& unpack_dir,
412 const std::string& input_ramdisk_path) {
413 // Simulate per-instance what the bootloader would usually do
414 // Since on other devices this runs every time, just do it here every time
415 std::ofstream final_rd(initrd_path,
416 std::ios_base::binary | std::ios_base::trunc);
417
418 std::ifstream boot_ramdisk(unpack_dir + "/ramdisk",
419 std::ios_base::binary);
420 std::string new_ramdisk_path = unpack_dir + "/vendor_ramdisk_repacked";
421 // Test to make sure new ramdisk hasn't already been repacked if input ramdisk is provided
422 if (FileExists(input_ramdisk_path) && !FileExists(new_ramdisk_path)) {
423 RepackVendorRamdisk(input_ramdisk_path,
424 unpack_dir + "/" + kConcatenatedVendorRamdisk,
425 new_ramdisk_path, unpack_dir);
426 }
427 std::ifstream vendor_boot_ramdisk(FileExists(new_ramdisk_path) ? new_ramdisk_path : unpack_dir +
428 "/concatenated_vendor_ramdisk",
429 std::ios_base::binary);
430
431 std::ifstream vendor_boot_bootconfig(unpack_dir + "/bootconfig",
432 std::ios_base::binary |
433 std::ios_base::ate);
434
435 auto vb_size = vendor_boot_bootconfig.tellg();
436 vendor_boot_bootconfig.seekg(0);
437
438 std::ifstream persistent_bootconfig(bootconfig_path,
439 std::ios_base::binary |
440 std::ios_base::ate);
441
442 auto pb_size = persistent_bootconfig.tellg();
443 persistent_bootconfig.seekg(0);
444
445 // Build the bootconfig string, trim it, and write the length, checksum
446 // and trailer bytes
447
448 std::string bootconfig =
449 "androidboot.slot_suffix=_a\n"
450 "androidboot.force_normal_boot=1\n"
451 "androidboot.verifiedbootstate=orange\n";
452 auto bootconfig_size = bootconfig.size();
453 bootconfig.resize(bootconfig_size + (uint64_t)(vb_size + pb_size), '\0');
454 vendor_boot_bootconfig.read(&bootconfig[bootconfig_size], vb_size);
455 persistent_bootconfig.read(&bootconfig[bootconfig_size + vb_size], pb_size);
456 // Trim the block size padding from the persistent bootconfig
457 bootconfig.erase(bootconfig.find_last_not_of('\0'));
458
459 // Write out the ramdisks and bootconfig blocks
460 final_rd << boot_ramdisk.rdbuf() << vendor_boot_ramdisk.rdbuf()
461 << bootconfig;
462
463 // Append bootconfig length
464 bootconfig_size = bootconfig.size();
465 final_rd.write(reinterpret_cast<const char *>(&bootconfig_size),
466 sizeof(uint32_t));
467
468 // Append bootconfig checksum
469 uint32_t bootconfig_csum = 0;
470 for (auto i = 0; i < bootconfig_size; i++) {
471 bootconfig_csum += bootconfig[i];
472 }
473 final_rd.write(reinterpret_cast<const char *>(&bootconfig_csum),
474 sizeof(uint32_t));
475
476 // Append bootconfig trailer
477 final_rd << "#BOOTCONFIG\n";
478 final_rd.close();
479 }
480
481 // TODO(290586882) switch this function to rely on avb footers instead of
482 // the os version field in the boot image header.
483 // https://source.android.com/docs/core/architecture/bootloader/boot-image-header
ReadAndroidVersionFromBootImage(const std::string & temp_dir_parent,const std::string & boot_image_path)484 Result<std::string> ReadAndroidVersionFromBootImage(
485 const std::string& temp_dir_parent, const std::string& boot_image_path) {
486 std::string tmp_dir = temp_dir_parent + "/XXXXXXX";
487 if (!mkdtemp(tmp_dir.data())) {
488 return CF_ERR("boot image unpack dir could not be created");
489 }
490 android::base::ScopeGuard delete_dir([tmp_dir]() {
491 Result<void> remove_res = RecursivelyRemoveDirectory(tmp_dir);
492 if (!remove_res.ok()) {
493 LOG(ERROR) << "Failed to delete temp dir '" << tmp_dir << '"';
494 LOG(ERROR) << remove_res.error().FormatForEnv();
495 }
496 });
497
498 CF_EXPECTF(GetAvbMetadataFromBootImage(boot_image_path, tmp_dir),
499 "'{}' boot image unpack into '{}' failed", boot_image_path,
500 tmp_dir);
501
502 std::string boot_params = ReadFile(tmp_dir + "/boot_params");
503
504 std::string os_version =
505 ExtractValue(boot_params, "Prop: com.android.build.boot.os_version -> ");
506 // if the OS version is "None", or the prop does not exist, it wasn't set
507 // when the boot image was made.
508 if (os_version == "None" || os_version.empty()) {
509 LOG(INFO) << "Could not extract os version from " << boot_image_path
510 << ". Defaulting to 0.0.0.";
511 return "0.0.0";
512 }
513
514 // os_version returned above is surrounded by single quotes. Removing the
515 // single quotes.
516 os_version.erase(remove(os_version.begin(), os_version.end(), '\''),
517 os_version.end());
518
519 std::regex re("[1-9][0-9]*([.][0-9]+)*");
520 CF_EXPECT(std::regex_match(os_version, re), "Version string is not a valid version \"" + os_version + "\"");
521 return os_version;
522 }
523 } // namespace cuttlefish
524