1 /*
2 * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8
9 #include "metadata_reader.h"
10 #include "protocols/service/fwu/metadata_v1.h"
11
12 class metadata_v1_reader : public metadata_version_specific_reader {
13 public:
14 metadata_v1_reader();
15 ~metadata_v1_reader();
16
17 bool is_supported(const uint8_t *buf, size_t data_len) const override;
18
19 void get_version(const uint8_t *buf, size_t data_len, unsigned int &version) const override;
20
21 void get_active_index(const uint8_t *buf, size_t data_len,
22 unsigned int &active_index) const override;
23 };
24
25 /* Registers on static construction */
26 static metadata_v1_reader the_v1_reader;
27
metadata_v1_reader()28 metadata_v1_reader::metadata_v1_reader()
29 : metadata_version_specific_reader()
30 {
31 metadata_reader::instance()->register_reader(this);
32 }
33
~metadata_v1_reader()34 metadata_v1_reader::~metadata_v1_reader()
35 {
36 }
37
is_supported(const uint8_t * buf,size_t data_len) const38 bool metadata_v1_reader::is_supported(const uint8_t *buf, size_t data_len) const
39 {
40 assert(buf);
41
42 const struct fwu_metadata *metadata = (const struct fwu_metadata *)buf;
43
44 return (data_len >= sizeof(struct fwu_metadata)) && (metadata->version == 1);
45 }
46
get_version(const uint8_t * buf,size_t data_len,unsigned int & version) const47 void metadata_v1_reader::get_version(const uint8_t *buf, size_t data_len,
48 unsigned int &version) const
49 {
50 assert(buf);
51 assert(data_len >= sizeof(struct fwu_metadata));
52
53 const struct fwu_metadata *metadata = (const struct fwu_metadata *)buf;
54
55 version = metadata->version;
56 }
57
get_active_index(const uint8_t * buf,size_t data_len,unsigned int & active_index) const58 void metadata_v1_reader::get_active_index(const uint8_t *buf, size_t data_len,
59 unsigned int &active_index) const
60 {
61 assert(buf);
62 assert(data_len >= sizeof(struct fwu_metadata));
63
64 const struct fwu_metadata *metadata = (const struct fwu_metadata *)buf;
65
66 active_index = metadata->active_index;
67 }