1 /*
2  * Copyright 2014-2017 Frank Hunleth
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 "fwup_metadata.h"
18 #include <confuse.h>
19 #include <stdlib.h>
20 
21 #include "cfgfile.h"
22 #include "cfgprint.h"
23 #include "util.h"
24 
list_one(cfg_t * cfg,const char * key,struct simple_string * s)25 static void list_one(cfg_t *cfg, const char *key, struct simple_string *s)
26 {
27     fwup_cfg_opt_to_string(cfg_getopt(cfg, key), s, false);
28 }
29 
list_metadata(cfg_t * cfg,struct simple_string * s)30 static void list_metadata(cfg_t *cfg, struct simple_string *s)
31 {
32     list_one(cfg, "meta-product", s);
33     list_one(cfg, "meta-description", s);
34     list_one(cfg, "meta-version", s);
35     list_one(cfg, "meta-author", s);
36     list_one(cfg, "meta-platform", s);
37     list_one(cfg, "meta-architecture", s);
38     list_one(cfg, "meta-creation-date", s);
39     list_one(cfg, "meta-fwup-version", s);
40     list_one(cfg, "meta-misc", s);
41     list_one(cfg, "meta-vcs-identifier", s);
42     list_one(cfg, "meta-uuid", s);
43 }
44 
45 /**
46  * @brief Dump the metadata in a firmware update file
47  * @param fw_filename the firmware update filename
48  * @param public_keys a list of public keys
49  * @param metadata_key if non-NULL, then print the value of the specified key
50  * @return 0 if successful
51  */
fwup_metadata(const char * fw_filename,unsigned char * const * public_keys,const char * metadata_key)52 int fwup_metadata(const char *fw_filename,
53                   unsigned char * const *public_keys,
54                   const char *metadata_key)
55 {
56     cfg_t *cfg;
57     if (cfgfile_parse_fw_meta_conf(fw_filename, &cfg, public_keys) < 0)
58         return -1;
59 
60     struct simple_string s;
61     simple_string_init(&s);
62 
63     if (metadata_key) {
64         ssprintf(&s, "%s\n", cfg_opt_getnstr(cfg_getopt(cfg, metadata_key), 0));
65     } else {
66         list_metadata(cfg, &s);
67     }
68 
69     cfgfile_free(cfg);
70 
71     fwup_output(FRAMING_TYPE_SUCCESS, 0, s.str);
72     free(s.str);
73     return 0;
74 }
75