1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <stdint.h>
8 #include <utility>
9 
10 #include <fbl/function.h>
11 #include <lib/zx/vmo.h>
12 #include <zircon/boot/bootdata.h>
13 #include <zircon/compiler.h>
14 #include <zircon/types.h>
15 
16 namespace bootfs {
17 
18 // A parser for the bootfs format
19 class Parser {
20 public:
21     // Create an empty Parser.
22     Parser() = default;
23     ~Parser();
24 
25     // Initializes a bootfs file system from |vmo|.
26     zx_status_t Init(zx::unowned_vmo vmo);
27 
28     // Parses the bootfs file system and calls |callback| for each |bootfs_entry_t|.
29     // If a callback returns something besides ZX_OK, the iteration stops.
30     using Callback = fbl::Function<zx_status_t(const bootfs_entry_t* entry)>;
31     zx_status_t Parse(Callback callback);
32 
33 private:
34     Parser(const Parser&) = delete;
35     Parser& operator=(const Parser&) = delete;
36 
Parser(Parser && other)37     Parser(Parser&& other) {
38         *this = std::move(other);
39     }
40     Parser& operator=(Parser&& other) {
41         dirsize_ = other.dirsize_;
42         dir_ = other.dir_;
43         other.dir_ = nullptr;
44         return *this;
45     }
46 
MappingSize()47     size_t MappingSize() const {
48         return dirsize_ + sizeof(bootfs_header_t);
49     }
50 
51     uint32_t dirsize_ = 0;
52     void* dir_ = nullptr;
53 };
54 
55 } // namespace bootfs
56