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 // This Header is a thin C++ wrapper around the C ZBI processing API provided in 6 // ulib/zbi/* 7 // 8 // Documentation for methods can also be found in ulib/zbi/include/zbi/zbi.h 9 10 #pragma once 11 12 #include "zbi.h" 13 14 #include <stddef.h> 15 #include <zircon/boot/image.h> 16 17 namespace zbi { 18 19 class Zbi { 20 public: Zbi(uint8_t * base)21 explicit Zbi(uint8_t* base) 22 : base_(base) { 23 zbi_header_t* hdr = reinterpret_cast<zbi_header_t*>(base_); 24 capacity_ = hdr->length + sizeof(*hdr); 25 } 26 Zbi(uint8_t * base,size_t capacity)27 Zbi(uint8_t* base, size_t capacity) 28 : base_(base), capacity_(capacity) {} 29 Reset()30 zbi_result_t Reset() { 31 return zbi_init(base_, capacity_); 32 } 33 Check(zbi_header_t ** err)34 zbi_result_t Check(zbi_header_t** err) const { 35 return zbi_check(base_, err); 36 } 37 38 zbi_result_t CheckComplete(zbi_header_t** err = nullptr) const { 39 return zbi_check_complete(base_, err); 40 } 41 ForEach(zbi_foreach_cb_t cb,void * cookie)42 zbi_result_t ForEach(zbi_foreach_cb_t cb, void* cookie) const { 43 return zbi_for_each(base_, cb, cookie); 44 } 45 AppendSection(uint32_t length,uint32_t type,uint32_t extra,uint32_t flags,const void * payload)46 zbi_result_t AppendSection(uint32_t length, uint32_t type, uint32_t extra, 47 uint32_t flags, const void* payload) { 48 return zbi_append_section(base_, capacity_, length, type, extra, flags, 49 payload); 50 } 51 CreateSection(uint32_t length,uint32_t type,uint32_t extra,uint32_t flags,void ** payload)52 zbi_result_t CreateSection(uint32_t length, uint32_t type, uint32_t extra, 53 uint32_t flags, void** payload) { 54 return zbi_create_section(base_, capacity_, length, type, extra, flags, 55 payload); 56 } 57 Base()58 const uint8_t* Base() const { return base_; }; Length()59 uint32_t Length() const { 60 return Header()->length + static_cast<uint32_t>(sizeof(zbi_header_t)); 61 } 62 63 protected: 64 uint8_t* base_ = nullptr; 65 size_t capacity_ = 0; 66 67 Zbi() = default; 68 Header()69 zbi_header_t* Header() { 70 return reinterpret_cast<zbi_header_t*>(base_); 71 } Header()72 const zbi_header_t* Header() const { 73 return reinterpret_cast<const zbi_header_t*>(base_); 74 } Payload()75 void* Payload() { 76 return reinterpret_cast<void*>(Header() + 1); 77 } 78 }; 79 80 } // namespace zbi 81